+ 1

What is 'self' in OOP?

I wish someone is kind enough to give me all what I must know about it

4th Apr 2019, 12:13 PM
wael barhoumi
wael barhoumi - avatar
3 Respostas
+ 7
Self is basically a replacement of this of other languages in python. It is mainly used to set the properties of objects while constructing them with a constructor. You can see its implementation in this code. https://code.sololearn.com/c2rTtBY7TWU9/?ref=app
4th Apr 2019, 2:44 PM
Arushi Singhania
Arushi Singhania - avatar
+ 4
A class definition is a 'construction plan' for several objects. If you have a class Food, you will want to build specific foods out of it; you'll have an apple, a pear, a banana... 'self' is the abstract word for 'the specific instance that will be made later with this construction plan'. This is to make sure that when later you try to define the caloric value of cheese, it will not be stored in lettuce or candy - but in cheese. This may not be 'all' you need to know - but hopefully a start. ;-)
4th Apr 2019, 12:22 PM
HonFu
HonFu - avatar
+ 2
Python? For class method involving only one operand, you use self. In magic operator, the collocation is to use self and other to denote the first and second operand https://code.sololearn.com/cKQT42Mk4BlQ/?ref=app But it is just a variable name, you can use a and b to replace self and other if you like https://code.sololearn.com/cBPSiPMWMF7b/?ref=app
4th Apr 2019, 1:13 PM
Gordon
Gordon - avatar