[pypy-dev] Access the name of variable that is being assigned

anatoly techtonik techtonik at gmail.com
Tue Jul 15 11:05:28 CEST 2014


Hi,

Is it possible at all to define a class in Python that
can read name of variable it is assigned to on init?

  >>> MyObject = SomeClass()
  >>> print(MyObject)
  'MyObject'

For this to work, SomeClass __init__ needs to know
what variable name is currently waiting to be
assigned. But at the time __init__ is executed,
MyObject is not entered global or local space yet.

I know that this is possible to do on AST level, but
AST is inaccessible when program is running, so
what is the corresponding structure to track that?
(links to source are appreciated)

1. Is it possible to do this in CPython and PyPy?
2. Is it possible to do this in generic way?
3. Is there any stack of assignments?
3.1. Is this stack accessible?

Thanks.
-- 
anatoly t.


More information about the pypy-dev mailing list