ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 06 Jun 2013 00:40:50 -0500How can I find out the name of some variable?http://ask.sagemath.org/question/10183/how-can-i-find-out-the-name-of-some-variable/Hello!
For example I have variable p in sage notebook. I need my class can find out the name of variable:
sage: p = 3
sage: my_class(p)
ans: 'p'
How to implement this feature? Thanks.
P.S.: sorry if my english isn't good.Wed, 05 Jun 2013 09:45:40 -0500http://ask.sagemath.org/question/10183/how-can-i-find-out-the-name-of-some-variable/Answer by tmonteil for <p>Hello!
For example I have variable p in sage notebook. I need my class can find out the name of variable:</p>
<pre><code>sage: p = 3
sage: my_class(p)
ans: 'p'
</code></pre>
<p>How to implement this feature? Thanks.
P.S.: sorry if my english isn't good.</p>
http://ask.sagemath.org/question/10183/how-can-i-find-out-the-name-of-some-variable/?answer=15029#post-id-15029You should understand that an object may have different names:
sage: p = 3
sage: q = p
sage: id(p)
529391024
sage: id(q)
529391024
Here, `p` is the name of the same object than `q` (i mean, they both are the name of the same location in memory). Hence, your question is not well defined, since Sage (or Python) will not be able to make a difference between `p` and `q`. If you need such a feature, there may a better way to write your progam.
That said, you can try to look for your variable in the `globals()` dictionary:
sage: def my_name(p):
....: for name, value in globals().items():
....: if id(value) == id(p):
....: return name
sage: my_name(p)
'p'
sage: my_name(q)
'p'
That said, this is quite a dirty method, and you should consider avoiding it.
Wed, 05 Jun 2013 10:45:05 -0500http://ask.sagemath.org/question/10183/how-can-i-find-out-the-name-of-some-variable/?answer=15029#post-id-15029Comment by AndreWin for <p>You should understand that an object may have different names:</p>
<pre><code>sage: p = 3
sage: q = p
sage: id(p)
529391024
sage: id(q)
529391024
</code></pre>
<p>Here, <code>p</code> is the name of the same object than <code>q</code> (i mean, they both are the name of the same location in memory). Hence, your question is not well defined, since Sage (or Python) will not be able to make a difference between <code>p</code> and <code>q</code>. If you need such a feature, there may a better way to write your progam.</p>
<p>That said, you can try to look for your variable in the <code>globals()</code> dictionary:</p>
<pre><code>sage: def my_name(p):
....: for name, value in globals().items():
....: if id(value) == id(p):
....: return name
sage: my_name(p)
'p'
sage: my_name(q)
'p'
</code></pre>
<p>That said, this is quite a dirty method, and you should consider avoiding it.</p>
http://ask.sagemath.org/question/10183/how-can-i-find-out-the-name-of-some-variable/?comment=17555#post-id-17555Thank you very much!)))Thu, 06 Jun 2013 00:40:50 -0500http://ask.sagemath.org/question/10183/how-can-i-find-out-the-name-of-some-variable/?comment=17555#post-id-17555