Explaining the 'Self' Variable to a Beginner

Explaining the 'self' variable to a beginner

I'll try to clear up some confusion about classes and objects for you first. Lets look at this block of code:

>>> class Bank(): # let's create a bank, building ATMs
... crisis = False
... def create_atm(self) :
... while not self.crisis :
... yield "$100"

The comment there is a bit deceptive. The above code does not "create" a bank. It defines what a bank is. A bank is something which has a property called crisis, and a function create_atm. That's what the above code says.

Now let's actually create a bank:

>>> x = Bank()

There, x is now a bank. x has a property crisis and a function create_atm. Calling x.create_atm(); in python is the same as calling Bank.create_atm(x);, so now self refers to x. If you add another bank called y, calling y.create_atm() will know to look at y's value of crisis, not x's since in that function self refers to y.

self is just a naming convention, but it is very good to stick with it. It's still worth pointing out that the code above is equivalent to:

>>> class Bank(): # let's create a bank, building ATMs
... crisis = False
... def create_atm(thisbank) :
... while not thisbank.crisis :
... yield "$100"

What is the purpose of the `self` parameter? Why is it needed?

The reason you need to use self. is because Python does not use special syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions, and leaves the actual name to use up to you (although self is the convention, and people will generally frown at you when you use something else.) self is not special to the code, it's just another object.

Python could have done something else to distinguish normal names from attributes -- special syntax like Ruby has, or requiring declarations like C++ and Java do, or perhaps something yet more different -- but it didn't. Python's all for making things explicit, making it obvious what's what, and although it doesn't do it entirely everywhere, it does do it for instance attributes. That's why assigning to an instance attribute needs to know what instance to assign to, and that's why it needs self..

Python - why use self in a class?

A.x is a class variable.
B's self.x is an instance variable.

i.e. A's x is shared between instances.

It would be easier to demonstrate the difference with something that can be modified like a list:

#!/usr/bin/env python

class A:
x = []
def add(self):
self.x.append(1)

class B:
def __init__(self):
self.x = []
def add(self):
self.x.append(1)

x = A()
y = A()
x.add()
y.add()
print("A's x:", x.x)

x = B()
y = B()
x.add()
y.add()
print("B's x:", x.x)

Output

A's x: [1, 1]
B's x: [1]

What is 'self' in the last parameter?

It's is the same thing as self anywhere else in the method. It is just a reference to the current instance.

self is just a naming convention; it is commonly used as the name for the first argument automatically passed to a method. Within the method, you can then use that name where ever you want to use the current instance; you can call methods on it, you can set attributes on it, or you can pass that reference on to other methods. See Explaining the python 'self' variable to a beginner.

In this case, the current instance is being passed into another method as argument. There is nothing special about it being passed in as the last argument; it is just another argument.



Related Topics



Leave a reply



Submit