tags:

views:

218

answers:

3

I don't understand what these are used for, particularly the self argument? Could some please explain this to me and why on earth you would want to pass this in?

Also, I've always thought __init__ was for 'initialisation', but it didn't occur to me that I've never had to put anything in here before. Could someone give me an easy example?

edit: i just get so confused everytime i see self being passed into a function, or something of the like.

+2  A: 

Think of it as the "this" parameter in C++ / Java : it is used to reference an instance of a class.

Class Whatever:   
  def __init__(self, some_parameter):   
    self.instance_parameter = some_parameter

Think of __init__ as a class instance constructor.

jldupont
+16  A: 

self is the object you're calling the method on. It's a bit like this in Java.

__init__ is called on each object when it is created to initialise it. It's like the constructor in Java.

So you would use __init__ whenever you wanted to set any attributes - member variables in Java - of the object when it was created. If you're happy with an "empty" object you don't need an __init__ method but if you want to create an object with arguments you'll need one.

An example would be:

class StackOverflowUser:
    def __init__(self, name, userid, rep): 
        self.name = name
        self.userid = userid
        self.rep = rep

dave = StackOverflowUser("Dave Webb",3171,500)

We can then look at the object we've created:

>>> dave.rep
500
>>> dave.name
'Dave Webb'

So we can see __init__ is passed the arguments we gave to the constructor along with self, which is the reference to the object that has been created. We then use self when we process the arguments and update the object appropriately.

There is the question of why Python has self when other languages don't need it. According to the Python FAQ:

Why must 'self' be used explicitly in method definitions and calls?

First, it's more obvious that you are using a method or instance attribute instead of a local variable...

Second, it means that no special syntax is necessary if you want to explicitly reference or call the method from a particular class...

Finally, for instance variables it solves a syntactic problem with assignment: since local variables in Python are (by definition!) those variables to which a value assigned in a function body (and that aren't explicitly declared global), there has to be some way to tell the interpreter that an assignment was meant to assign to an instance variable instead of to a local variable, and it should preferably be syntactic (for efficiency reasons)...

Dave Webb
+2  A: 

self is by convention the instance of the object that you are calling a method of.

For example:

class MyClass:
  """A simple example class"""
  def __init__(self):
    print "MyClass class created"
  i = 12345
  def f(self):
    return self.i

# Create an object called a of type MyClass. This will print "MyClass class created"
a = MyClass()

# Run the method - should return 12345
print a.f()

# Make another instance of the class to illustrate they are separate.
b = MyClass()
# Set a.i to another value.
a.i = 1

# Note the result
print a.f()
print b.f()

By the way, you don't have to call it self, you can call it whatever you like. It will always be the first parameter.

rjmunro
i see, the 'self' would change the scope if you'd like? in order to say "im going to access this value `i` , but first i need to say where it is, as it's not local to method?"
sm1991