Im not the OP, but this clears up some stuff for ma about "self".
I thank you for your time to post this.
-shawn

On 7/31/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
Sebastian Smith wrote:
> I am new to Python, real new. I am loving the language and learning
> fast but I have hit a wall with the 'self'. I have Googled and
> searched and read a few definitions but it still doesn't make sense to
> me.
>
> I would love to hear some of your 'layman's definitions' on the self.
>
> Thank you all,
>
> Ben.
> _______________________________________________
> Tutor maillist  -   [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
>
Consider this:
#--start code
def f():
    print 'hello'
>>> a = f
>>> a
<function f at 0x00AE67F0>
>>> a()
hello
>>> f
<function f at 0x00AE67F0>
>>> f()
hello
#-- end code

See how whenever you define a function, it creates a 'function' object?
This means you can do anything with the function, assign a variable to it,
put it in a list or a dictionary, etc.
we assigned the variable 'a' to the function 'f', so we could call
function 'f' using either 'a()' or 'f()'.
The reason we're allowed to have as many variables pointing to the same
function as we want is because the function always does the same thing
when passed the same parameters (more or less.)
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.
Consider the following class:
#-- start code
Class Address(object):
    def __init__(street,city,zip):
       print "Street: %s, City: %s, Zip: %s" % (street,city,zip)
#-- end code
We can do whatever we want with these variables inside of our __init__
function.
If we call the function, using Address.__init__('1232 west highway
boulevard','indianapolis','10000')
it will print the string 'Street: 1232 west highway boulevard, City:
indianapolis, Zip: 10000'
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, so that we don't have to change the original.
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:
>>> 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.
Luke
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to