What is "self"?

Sam Pointon free.condiments at gmail.com
Thu Sep 22 22:16:11 EDT 2005


self is the class instance that the bound function being called belongs
to. This example should illustrate a bit.


class Foo(object):
    def __init__(self, value):
        self.value = value # so the Foo instance now has an attribute,
value

    def get_value(self):
        return self.value # This gets the previously-set value
attribute of the Foo instance

bar = Foo(42)
baz = Foo('101010')

print bar.get_value() #Note that the self argument is implicit since
this is a bound method.
print
print baz.get_value()

The output is (or should be, as this is untested):
42

101010




More information about the Python-list mailing list