ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 19 Apr 2013 21:09:44 +0200Showing options after writing "." when you choose an element of a listhttps://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/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]"?Thu, 18 Apr 2013 06:38:14 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/Answer by Luiz Roberto Meier for <p>Let me illustrate my question with a particular (naive) example of a list.</p>
<pre><code>sage: a=graphs.CompleteGraph(3)
sage: b=graphs.CompleteGraph(4)
sage: c=[a,b]
</code></pre>
<p>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.).</p>
<p>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]"?</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?answer=14804#post-id-14804Will be practical for you to use a loop with this inside:
var('z')
some loop here
...
z = c[0]
z. <TAB> will work.
Fri, 19 Apr 2013 04:56:18 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?answer=14804#post-id-14804Comment by tmonteil for <p>Will be practical for you to use a loop with this inside:</p>
<p>var('z')</p>
<p>some loop here</p>
<p>...</p>
<p>z = c[0]</p>
<p>z. <tab> will work.</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17857#post-id-17857Warning !
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.
Fri, 19 Apr 2013 05:54:07 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17857#post-id-17857Comment by Luiz Roberto Meier for <p>Will be practical for you to use a loop with this inside:</p>
<p>var('z')</p>
<p>some loop here</p>
<p>...</p>
<p>z = c[0]</p>
<p>z. <tab> will work.</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17846#post-id-17846Thank you.Fri, 19 Apr 2013 21:09:44 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17846#post-id-17846Answer by tmonteil for <p>Let me illustrate my question with a particular (naive) example of a list.</p>
<pre><code>sage: a=graphs.CompleteGraph(3)
sage: b=graphs.CompleteGraph(4)
sage: c=[a,b]
</code></pre>
<p>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.).</p>
<p>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]"?</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?answer=14802#post-id-14802Hi,
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.
Thu, 18 Apr 2013 07:29:30 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?answer=14802#post-id-14802Answer by Volker Braun for <p>Let me illustrate my question with a particular (naive) example of a list.</p>
<pre><code>sage: a=graphs.CompleteGraph(3)
sage: b=graphs.CompleteGraph(4)
sage: c=[a,b]
</code></pre>
<p>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.).</p>
<p>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]"?</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?answer=14801#post-id-14801`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.Thu, 18 Apr 2013 07:02:19 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?answer=14801#post-id-14801Comment by ppurka for <p><code>c[i]</code> is just shorthand for <code>c.__getitem__(i)</code>. The <code>__getitem__</code> 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 <code>__getitem__</code> without actually calling it.</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17863#post-id-17863No. 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 :)Thu, 18 Apr 2013 07:59:36 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17863#post-id-17863Comment by boumol for <p><code>c[i]</code> is just shorthand for <code>c.__getitem__(i)</code>. The <code>__getitem__</code> 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 <code>__getitem__</code> without actually calling it.</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17862#post-id-17862Thanks 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?Thu, 18 Apr 2013 11:15:22 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17862#post-id-17862Comment by ppurka for <p><code>c[i]</code> is just shorthand for <code>c.__getitem__(i)</code>. The <code>__getitem__</code> 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 <code>__getitem__</code> without actually calling it.</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17861#post-id-17861That'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.Thu, 18 Apr 2013 13:56:41 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17861#post-id-17861Comment by boumol for <p><code>c[i]</code> is just shorthand for <code>c.__getitem__(i)</code>. The <code>__getitem__</code> 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 <code>__getitem__</code> without actually calling it.</p>
https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17864#post-id-17864So, 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]".Thu, 18 Apr 2013 07:16:00 +0200https://ask.sagemath.org/question/10036/showing-options-after-writing-when-you-choose-an-element-of-a-list/?comment=17864#post-id-17864