While everyone is saying what they want in Python :)

Aahz Maruch aahz at panix.com
Sun Feb 4 13:31:11 EST 2001


In article <slrn97r8nn.gp.warlock at gargoyle.myth>,
Jim Richardson <warlock at eskimo.com> wrote:
>
>can someone explain to me what exactly "self" is for? I just don't get
>it.  (I am trying to learn this language, but this puzzles me.) Every
>time I think I get it, I find proof otherwise :)

Let's start by considering a class:

class foo:
    pass

To create an instance of the class we call it like a function:

a = foo()
b = foo()

We now have two instances of the class.  Normally classes have functions
associated with them (called methods):

class foo:
    def __init__(self, bar):
        self.bar = bar
    def print(self):
        x = type(self.bar)
        print self.bar, "is type", x

In order to create instances of this new class, we do this:

a = foo(5)
b = foo('Hi there!')

In order to call the print() method of foo, we do this:

a.print()
b.print()

Now, inside the print() method, how is Python supposed to distinguish
between a variable that belongs to the instance versus one that is
strictly local to the method (as x is)?
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"People sometimes focus too much on the fact of communication rather than
the substance of communication."  --Dave Morton



More information about the Python-list mailing list