What is self ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is self ?

10th Dec 2016, 5:38 PM
Ashfaque Mohammed Abu
Ashfaque Mohammed Abu - avatar
7 Answers
+ 2
same as "this" in other languages
10th Dec 2016, 8:23 PM
Rishi Anand
Rishi Anand - avatar
+ 2
Rishi Anand raises a good point. Since, in Python, you explicitly declare the 'self' parameter you could use any identifier, including 'this'. Using 'self' is just a convention that helps programmers understand each other's code. Of course you would need to know what 'this' means in the languages that use 'this' instead of 'self'.
11th Dec 2016, 3:33 PM
Gordon Garmaise
Gordon Garmaise - avatar
+ 2
I understand... I just made an analogy. Here is the explanation. You do make classes in python. And create objects as instance of those classes. self or this, is used to refer to that instantiated objected.
11th Dec 2016, 7:18 PM
Rishi Anand
Rishi Anand - avatar
+ 1
'self' must be declared as the first parameter in class member functions. It is a reference to the instance of the class on which the member function was invoked (called). For example: inst0 = clas(); inst0.func(); When 'func' needs to use a member 'mem' of 'inst' the expression 'self.mem' is used to refer to 'inst0.mem'. Elsewhere in the program: inst1 = clas(); inst1.func(); Now when 'func' executes 'self.mem' will be referring to 'inst1.mem'.
10th Dec 2016, 6:09 PM
Gordon Garmaise
Gordon Garmaise - avatar
+ 1
It's Rishi, Gordon...
11th Dec 2016, 4:13 PM
Rishi Anand
Rishi Anand - avatar
0
I still don't understand but thanks for trying though
11th Dec 2016, 4:25 PM
Ashfaque Mohammed Abu
Ashfaque Mohammed Abu - avatar
0
Sorry for the typo Rishi. Am am not great with a cellphone keyboard. Respects Gordon
11th Dec 2016, 8:52 PM
Gordon Garmaise
Gordon Garmaise - avatar