| 1 | initial version |
You 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 really the same as q (i mean, they have 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() 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.
| 2 | No.2 Revision |
You 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 really the name of the same as object than q (i mean, they have 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() 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.
| 3 | No.3 Revision |
You 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 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() 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.
| 4 | No.4 Revision |
You 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() 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.
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.