Eric Abrahamsen wrote:
> When instantiating a class, is it possible for that instance to  
> 'know' (via the __init__ method, I suppose) the name of the variable  
> it's been assigned to?

This is pretty hard. For one thing, the object will not be bound to a 
name until after __init__() is finished. Second, the object may not be 
bound to a name at all; it could be created and thrown away, or it could 
be added to a list, dict or other container, it could be a member of 
another object, it could be a temporary created as a function argument 
or the return value of a function...

Even simple assignment has its complications. The assigned variable can 
be local or global. The assignment could be via tuple assignment in 
which case the value is inserted into a temporary tuple, then unpacked 
and assigned.

In the very simple case of
x = MyClass()

you could probably use the stack frame to find the point of call and 
inspect the byte codes there to find the name...

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to