[Tutor] What self is this ?

Kent Johnson kent_johnson at skillsoft.com
Wed Sep 22 19:51:56 CEST 2004


I don't think you can do that. The name you want is not a property of the 
instance, it is a property of the namespace where the instance is bound. A 
particular instance could be bound to 0, 1 or more names. For example,

aList = [XYZ()]
aList[0].who()

aList contains an XYZ that is not bound to any name; what should print? 
(Stricly speaking, aList is bound to a list that contains an XYZ.) Or how about

test1 = XYZ()
test2 = test1
test2.who()

test1 and test2 are bound to the same XYZ instance; what should print?

If you give us an idea what it is you are trying to accomplish maybe we can 
figure out a different way to do it.

Kent

At 10:36 AM 9/22/2004 -0700, Ertl, John wrote:
>I was working on a class and I want to assign the class instance name (if
>that is the correct term) to an attribute in the class instance.
>
>For example
>
>class XZY:
>         def __init__(self):
>                 self.instenceName = ????
>
>         def who(self):
>                 Print self.instenceName
>
>
>test1 = XZY()
>
>I want test1.who() to print test1
>
>I know this looks very "Why would you do that?"  but I am trying to give a
>simple example.  This is coming up in the context of a list of classes that
>is going to be iterated over.
>
>Thanks,
>
>John Ertl
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list