"Eric Abrahamsen" <[EMAIL PROTECTED]> 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?

You could pass it in as a string but there is little point.
Recall that in Python variables are just names. You can
have several variables pointing at the same object,
which 'name' should the instance use?

class C: pass

a = C()
b = a
d = b
L = []
L.appand(a)
L.append(b)
a = 42

Now we have 1 instance but 4 references to it and the
original name 'a' no longer points to it!.

Which name should it return?

Presumably you have a reason for asking this?
If you tell us what you are trying to do we might be able to
come up with a more conventional solution.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



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

Reply via email to