On Tue, Jul 15, 2014 at 12:50 PM, Yichao Yu <yyc1...@gmail.com> wrote: > 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())
I don't need this case, so I can ignore it. But reliably detecting this to distinguish from other situations would be nice. > or > some_other_object.some_attribute = SomeClass() Detect that name is an attribute, handle distinctly if needed, for my purpose print 'some_other_object.some_attribute' > or > some_variable = another_variable = SomeClass() Print closest assigned variable name, i.e. 'another_variable' > or > some_variable = (SomeClass(),) There is no direct assignment. Don't need this. > or even > SomeClass() # not assigning to anything > etc.... Good to detect. Don't need. Actually all cases that I don't need are the same case on this one - there is no direct assignment to variable. > I guess it would be better if you can describe what you really want to do. I described. Or you need use case or user story? I think I want to link object instances to variable names without to print those names (if possible) in __repr__ and debug messages to save time on troubleshooting. _______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev