+ 12

Why do we use only 'self' as a referrence in python and not anything else?

Using 'self' is very much common in python! But I'd like to ask the specific reason for the use of only 'self' as a reference in python? I'm sure that ‘self' isn't a predefined keyword in python! Then why only ‘self'? I've tried coding a class in python without using ‘self' word, and it runned successfully(without any difference)! If you need to see an Example Code of Class, here it is : https://code.sololearn.com/cl5eSbe58rJH/?ref=app Please share your views on this!

18th Apr 2017, 7:40 AM
Harshit Gupta
Harshit Gupta - avatar
4 Respostas
+ 17
Its just a convention, isn't it? like why are all ASCII characters numbered the way they are? Why do we use standardised units of measurement in scientific studies? By using standards, specifications and conventions, it means that anyone can understand how it works. Someone learning OOP, for example, might look at your code and see you've used "cats" instead of "self" and could be wondering "what's 'cats'? where is that defined?". whereas if you had used self like everyone else does, they'd know that its referring to the class's own instance.... probably.
18th Apr 2017, 8:00 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
+ 10
Thanks @Aidan Haddon-Wright and @Utpal Savliya for your Answers @Utpal Savliya, Yup! I'll surely relook the passed-on context (You got me bud)! Honestly saying, I didn't thought the initial one's were that important! Will check the necessary ones ASAP! Cheers to our Bottle's Caps! ★_★
19th Apr 2017, 6:56 AM
Harshit Gupta
Harshit Gupta - avatar
+ 3
@Harshit Gupta As the class methods are just functions, the names of parameters doesn't matter. We can use any other word instead of self, to refer to the instance o the class calling the method. It's just a convention to use 'self' and 'cls' in python. As I know, you just took shortcuts to pass the course, I suggest that you must study the modules once more. Especially in this context, I would suggest you to study The Object Oriented Programming, specially its first chapter. I Hope that would help. Utpal Rox(Rocks) ! [Cheers with my bottle's cap] (^^)^_^😁😎😂😉
19th Apr 2017, 5:31 AM
Utpal Savliya
Utpal Savliya - avatar
0
I think you should have posted this on my thread. - Python Doubts Or at least now provide the link to this thread there, as it will help coders to understand the language. Also, see your question. It says RunNED instead of RAn. https://www.sololearn.com/discuss/311455/?ref=app Cheers Again.
19th Apr 2017, 9:23 AM
Utpal Savliya
Utpal Savliya - avatar