Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explaining the 'self' variable to a beginner [duplicate]

I'm pretty much ignorant of OOP jargon and concepts. I know conceptually what an object is, and that objects have methods. I even understand that in python, classes are objects! That's cool, I just don't know what it means. It isn't clicking with me.

I'm currently trying to understand a few detailed answers that I think will illuminate my understanding of python:

  1. What does the "yield" keyword do in Python?
  2. What is a metaclass in Python?

In the first answer, the author uses the following code as an example:

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

I don't immediately grok what self is pointing to. This is definitely a symptom of not understanding classes, which I will work on at some point. To clarify, in

>>> def func(): ...   for i in range(3): ...     print i 

I understand that i points to an item in the list range(3) which, since it is in a function, isn't global. But what does self "point to"?

like image 555
jrhorn424 Avatar asked Aug 09 '11 00:08

jrhorn424


People also ask

What is a self variable?

The self variable is used to represent the instance of the class which is often used in object-oriented programming. It works as a reference to the object. Python uses the self parameter to refer to instance attributes and methods of the class.

What is __ init __? Explain with example?

The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.

Is self the same as this?

Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.

What is self in Python with example?

The self keyword is used to represent an instance (object) of the given class. In this case, the two Cat objects cat1 and cat2 have their own name and age attributes. If there was no self argument, the same class couldn't hold the information for both these objects.


2 Answers

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" 
like image 133
Paul Avatar answered Oct 04 '22 15:10

Paul


It may help you to think of the obj.method(arg1, arg2) invocation syntax as purely syntactic sugar for calling method(obj, arg1, arg2) (except that method is looked up via obj's type, and isn't global).

If you view it that way, obj is the first argument to the function, which traditionally is named self in the parameter list. (You can, in fact, name it something else, and your code will work correctly, but other Python coders will frown at you.)

like image 41
Chris Jester-Young Avatar answered Oct 04 '22 15:10

Chris Jester-Young