On Tue, Jul 15, 2014 at 5:05 PM, anatoly techtonik <techto...@gmail.com> wrote: > 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'
I thing in general a normal object in Python does not have a name and there's nothing special about the name of the variable it is assigned to first. To see why is is not going to work, what do you expect you print function to do if the object is created like some_function(SomeClass()) or some_other_object.some_attribute = SomeClass() or some_variable = another_variable = SomeClass() or some_variable = (SomeClass(),) or even SomeClass() # not assigning to anything etc.... I guess it would be better if you can describe what you really want to do. > > 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. > _______________________________________________ > pypy-dev mailing list > pypy-dev@python.org > https://mail.python.org/mailman/listinfo/pypy-dev _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev