[Tutor] Re: What self is this ?

Abel Daniel abli at freemail.hu
Thu Sep 23 18:33:37 CEST 2004

"Ertl, John" writes:

> 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

ok, how about:

   test2 = test1 = XZY()

After that, test2 is test1 (the names 'test1' and 'test2' refer to the
same object, to see that try 'print test1' you will get something like
<__main__.XYZ instance at 0x40517b0c> that octal number will be the
same for test1 and test2), would test1.who() print test1 or test2?

and how about:


what do you think should be printed?

You should really read http://effbot.org/zone/python-objects.htm

> 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.

When you are trying to do something like this, its a sure sign that
your program's design needs to be changed.

Abel Daniel

More information about the Tutor mailing list