Possible Duplicate:
Python 'self' keyword
Forgive me if this is an incredibly noobish question, but I never did understand self in Python. What does it do? And when I see things like
def example(self, args):
return self.something
what do they do? I think I've seen args somewhere in a function too. Please explain in a simple way :P
self
is the reference to the instance of the class that the method (theexample
function in this case) is of.You'll want to take a look at the Python docs on the class system for a full introduction to Python's class system. You'll also want to look at these answers to other questions about the subject on Stackoverflow.
It sounds like you've stumbled onto the object oriented features of Python.
self
is a reference to an object. It's very close to the concept ofthis
in many C-style languages. Check out this code:Self it a reference to the instance of the current class. In your example,
self.something
references thesomething
property of theexample
class object.