instance references?

Alex Martelli aleax at mail.comcast.net
Mon Jan 30 01:03:12 CET 2006


bytecolor <bytecolor at yahoo.com> wrote:
   ...
> So how can I tell if an instance of point, line or circle has actually
> been assigned to a variable?

You can't really distinguish whether an object has been assigned to a
variable, an item in a list, an attribute, etc etc -- all of these
assignments are much the same thing, there is nothing special about
variables to distinguish them from the other kind of 'labels'.  Indeed,
for example, global variables are EXACTLY the same thing as attributes
of a module objects which in turn are exactly the same things as values
in the module's __dict__ -- so, any hopes of distinguishing between
variables, attributes, and items inside a container, must clearly be
abandoned.

You can do various other things that might come close to what you want,
such as using weak references in your list (if no 'true' reference
exists, an object goes away and _weak_ references to that object let you
check if the object is still around or has gone away) -- that will let
you know whether the object has been assigned to *anything* (you can
perform a similar task by checking on the number of references extant to
an object).  But checking if the existing references are indeed
'variables' or something else is a hard, unrewarding task.


Alex
 



More information about the Python-list mailing list