Ask Your Question
1

Showing options after writing "." when you choose an element of a list

asked 2013-04-18 06:38:14 +0100

boumol gravatar image

Let me illustrate my question with a particular (naive) example of a list.

sage: a=graphs.CompleteGraph(3)
sage: b=graphs.CompleteGraph(4)
sage: c=[a,b]

In this situation if in a new line one writes down "a." and press "Tab", then one gets a list of all options to be used for the object a (for example "a.category", "a.center", etc.).

On the other hand, if in a new line one writes down "c[0]." and press "Tab", then one does not get the right feedback. Is there some way to make this work like in the previous paragraph (and display the options for the object "c[0]"?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2013-04-18 07:02:19 +0100

Volker Braun gravatar image

c[i] is just shorthand for c.__getitem__(i). The __getitem__ is of course fast for lists but could be arbitrarily slow and/or have side effects for custom objects. Since Python is not statically typed, there is no way of knowing the output of __getitem__ without actually calling it.

edit flag offensive delete link more

Comments

So, there is no way to run some sage command that tells that the list c I have just created is a list of graphs (in this particular case)? I guess that if we could do this, then it might be the case that later it works the completion for "c[0]".

boumol gravatar imageboumol ( 2013-04-18 07:16:00 +0100 )edit

No. A list is a python object. It can contain any other objects, but the list itself is oblivious to what it contains. I suppose that if you want to make it sentient, you will have to create your own list class :)

ppurka gravatar imageppurka ( 2013-04-18 07:59:36 +0100 )edit

Thanks for your answer. Then, I imagine that this should be something to add to the "to do list" for the sage notebook, since in the notebook this would make sense (first evaluate "c[0]" and then show the list of options for this evaluation). Do you think this suggestion makes sense?

boumol gravatar imageboumol ( 2013-04-18 11:15:22 +0100 )edit

That's reasonable only in certain cases. You are thinking only of c[0]. What if we use c[100] where c is not a list but a more complicated object? Sometimes, even computing the 100th element might take time. In the most generic cases, in the absence of any nice algorithm, Sage just falls back to enumerating all elements, putting them in a list and then outputting the 100th element. The <tab> completion on the other hand should be as instantaneous as possible.

ppurka gravatar imageppurka ( 2013-04-18 13:56:41 +0100 )edit
5

answered 2013-04-18 07:29:30 +0100

tmonteil gravatar image

Hi,

first notice that the same problem will appear if you type:

sage: a=graphs.CompleteGraph(3) ; a.<TAB>

The reason is that the Sage interpreter has to know what object is a to give you available methods. So, c[0] has to be evaluated somehow.

Now, if your aim is to avoid coming back to the beginnig of the line and replace

sage: c[0].

by

sage: d = c[0]

and then

sage: d.<TAB>

A trick that could help you is to use the underscore, which holds the result of the last evaluated command:

sage: c[0]
Complete graph: Graph on 3 vertices
sage: _.<TAB>

Hence, you only have to hit two additional keys ("evaluate" after c[0] and "underscore" before dot), no backtrack.

edit flag offensive delete link more
0

answered 2013-04-19 04:56:18 +0100

Luiz Roberto Meier gravatar image

Will be practical for you to use a loop with this inside:

var('z')

some loop here

...

z = c[0]

z. <tab> will work.

edit flag offensive delete link more

Comments

Warning ! var('z') is definitely not related to that question. It is used when you want to create an element of the symbolic ring: sage: z.parent() Symbolic Ring If you want to give to create a variable `z` whose content is `c[0]`, you just have to type: z = c[0] and `z` will be created on the fly.

tmonteil gravatar imagetmonteil ( 2013-04-19 05:54:07 +0100 )edit

Thank you.

Luiz Roberto Meier gravatar imageLuiz Roberto Meier ( 2013-04-19 21:09:44 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-04-18 06:38:14 +0100

Seen: 661 times

Last updated: Apr 19 '13