self naming class?

Luigi Ballabio ballabio at mac.com
Thu Oct 18 17:19:36 CEST 2001


At 10:05 PM 10/18/01 +1000, Serge Rey wrote:
>i'm trying to create a class that assigns a name attribute in such a way
>that the name is set equal to the name of the instance. for example:
>
>test = MyClass()
>
>so that test.name == "test"

Serge,
         sorry I'm not here with an answer to your question.
I see a phylosophical problem with the above, which is, "test" is not the 
name of your instance but rather the name of a reference to such instance.
What I mean is, assume that you find out how to make it work: you will have 
the problem that if you write

 >>> test = MyClass()
 >>> test2 = test

test and test2 are two references that point to the same instance, and 
you're in big trouble _defining_ what test.name is in this case. It would 
seem reasonable to expect

 >>> print test.name
'test'
 >>> print test2.name
'test2'

but this is not possible as they point to the same object, and test.name 
will at all times be the same as test2.name. This is also true for ids: 
id(test) is equal to id(test2), so that I don't think one has a way to 
distinguish the two instances....

Sorry for increasing your problems rather than decreasing them,
                                                                 Luigi





More information about the Python-list mailing list