I don't understand the purpose of "self" | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 6

I don't understand the purpose of "self"

I don't understand why we should add the "self" parameter in "def__init__(self)"

5th Mar 2018, 7:16 AM
K.F
K.F - avatar
5 ответов
+ 8
'self' means instance of your class. Let me explain a little bit. Say you have a class named Hello and method named helloFunction, then code would be like this: class Hello(): def helloFunction(): print('Hello World') right? dont include 'self' yet. Then when you want to call helloFunction, code would be: Hello.helloFunction(). Ok?! no problem right? It will print 'Hello World' because python interpreter knows that you are calling 'helloFunction' of 'Hello' class. No magic here. Then you creat object from your class. myObject = Hello() then call the function again: myObject.helloFunction() Then error will occur! why? because a class is the blue print of many objects right? So, 'Hello' class can create many objects from it by instantiation. So you can create another object from Hello class like: mySecondObject = Hello() . All perfect & clear. Then you call the function again. mySecondObject.helloFunction(). Now, helloFunction confuse about whom is calling him (i.e whether myObject or mySecondObject) . It cannot assume who is calling him! To let method to assume that it is called by myObject or mySecondObject, 'self' keyword must be included as method paremeter. So, now helloFunction can know which object is calling and safely print its code block. It is same logic with __init__ . because __init__ is a method, too! It is a method which execute when you create objects from class like above code. That's it! I hope this helps you.
5th Mar 2018, 7:41 AM
Sylar
+ 8
a very good question! Since Python forces self as first arg of instance methods, it should add it itself. Other oop languages dont need self as parameter. @sylar please correct if i am wrong. looks you have good knowledgr
5th Mar 2018, 8:18 AM
Oma Falk
Oma Falk - avatar
+ 3
The "self" parameter is an object, and its purpose is to call the method of the class. For example, in the code below: class Cat: def __init__(self, color): self.color = color the class is "Cat" and the method is "color". So, for any cat animal , you have the corresponding color. It's mandatory !!!
5th Mar 2018, 9:14 AM
Tanoh Le Kadiographe
Tanoh Le Kadiographe - avatar
+ 2
'self' is the instance of a class, for example: #We create a class called "People" class People(): def __init__(self, name) self.name = name #We add an instance called "person1" person1=People('ilai') If we would like to get the self.name of person1, we will need to write person1.name because person1 is the name of the instance, as simple as that. I hope my explanation helps you.
5th Mar 2018, 5:04 PM
Ilai Segev
Ilai Segev - avatar
+ 1
@OmaFalk You're right. It should. But main difference between python and some other OOP language is that, in python, everything is objects as you may already know it. Everything include classes, metaclasses, functions, and so on right? In some other OOPs, if you want to use a method inside a class and if that method is not static, then you need to instantiate that class first before you use it. Because static block (i.e static void main(), in which all code execute) can contain only static. So, if you use class name to get non-static method or variable, it will give you an error. Not only that, static are shared across within a same namespace. Now python, which give us more flexible. Because class is an object, you can just use dot notation to access some method. No need to worry about to be static. If you want your method as object's attribute, just say it with 'self'. Then class cannot access it (Only its instance can) . And you know what, if you want your method to be available to your class as well as object, then just add @staticmethod decorator above your method and you are good to go! PS. I admit that i little know about other OOPs (I did C# for about 1 year and some java) Not more than that. So, i may be wrong. I just answer with my current understanding (which is poor) about OOP. So, others may know more than me. But i hope this helpful to you :) Cheers!
5th Mar 2018, 3:39 PM
Sylar