[Tutor] The Self

Alan Gauld alan.gauld at btinternet.com
Mon Jul 31 23:35:11 CEST 2006


"Luke Paireepinart" <rabidpoobear at gmail.com> wrote
>> I would love to hear some of your 'layman's definitions' on the 
>> self.

> However, when you have a 'Class' object, it has its own collection
> of variables that it can modify, and its own set of functions that 
> work
> on its variables.  Because of this, you might want one class to do
> a certain thing and another class to do something else.

Thus is all true and good.

> Class Address(object):
>    def __init__(street,city,zip):
>       print "Street: %s, City: %s, Zip: %s" % (street,city,zip)

> That's cool, but what if we want to store the values of these 
> attributes?
> Well, if we're going to store the values, we'll want to make a 
> separate
> copy of the class  that we can modify,

But this isn't.
Its very wrong to think of creating instamnces as being copies of the 
class.
classes are objects in the python sense in their own right and its 
possible
to instantiate them and also possible to copy them, and the two are
different!

One way to copy a class is to create a blank class that inherits from
the one we want to copy:

>>> class Thing:
...  def __init__(self,a=42):
...   self.a = a
...  def speak(self):
...   print "I am a thing with value", self.a
...
>>> class Different(Thing): pass
...
>>> t = Thing()
>>> t2 = Thing(27)
>>> d = Different(666)
>>> for it in [t,t2,d]: it.speak()
...
I am a thing with value 42
I am a thing with value 27
I am a thing with value 666
>>>

Notice that d thinks it is a Thing too...

But we have two instances ofThing and one instance of Different.
Which brings us back to the OPs point about the use of self.
Now if we read Luke's response but use instance instead of
copy then the reply is correct. :-)


> This is where self comes in.
> #-- start code
> Class Address(object):
>    def __init__(self,street,city,zip):
>       self.street = street
>       self.city = city
>       self.zip = zip
>    def output(self):
>       print "Street: %s, City: %s, Zip: %s" %
> (self.street,self.city,self.zip)
> #-- end code
> Now we can make a separate copy of the Address class, called
> an 'instance' of the class, like this:

As Luke says we create instances, but instances are not copies.
I am of class Man, but I am jot a copy of a man I am an instance,
just as my father is and many others. Classes are common nouns
instances are proper nouns in grammatical terms.

> >>> address1 = Address('1232 lexington drive','Annapolis','32423')
> when we call the output method of address1 we see:
> >>> address1.output()
> Street: 1232 lexington drive, City: Annapolis, Zip: 32423
> Now say we want to change the city name, cause we meant to put
> 'Indianapolis.'  All we have to do is this:
> address1.city = 'Indianapolis'
> Basically, when you call any function inside of a class, using a 
> class
> instance,
> (calling address1.anything is calling a function in an instance of a 
> class,
> calling Address.anything is calling the function from the original 
> copy
> of the class.)
> the first argument passed will be 'self', which is just the entire
> contents of the class.
> Hope that helps, I'd go more in-depth but I gotta run.

Likewise :-)
But you can read more on my web site OOP topic under the
heading "What is self".

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





More information about the Tutor mailing list