What is the purpose of self?

0 votes

What is the purpose of the self word in Python? I understand it refers to the specific object created from that class, but I can't see why it explicitly needs to be added to every function as a parameter. To illustrate, in Ruby I can do this:

class myClass def myFunc(name) @name = name end end

Which I understand, quite easily. However in Python I need to include self:

class myClass: def myFunc(self, name): self.name = name

Can anyone talk me through this? It is not something I've come across in my (admittedly limited) experience.

Aug 3, 2018 in Python by bug_seeker
• 15,350 points
23 views

1 answer to this question.

0 votes

The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions, and leaves the actual name to use up to you (although self is the convention, and people will generally frown at you when you use something else.) self is not special to the code, it's just another object.

Python could have done something else to distinguish normal names from attributes -- special syntax like Ruby has, or requiring declarations like C++ and Java do, or perhaps something yet more different -- but it didn't. Python's all for making things explicit, making it obvious what's what, and although it doesn't do it entirely everywhere, it does do it for instance attributes. That's why assigning to an instance attribute needs to know what instance to assign to, and that's why it needs self.

answered Aug 3, 2018 by Priyaj
• 56,520 points

Related Questions In Python

0 votes
1 answer

What is the purpose of inner class in Python?

Advantages of inner class: Logical grouping of classes: ...READ MORE

answered Feb 7 in Python by SDeb
• 13,160 points
184 views
0 votes
1 answer

What is the purpose of Zope Interfaces?

You can actually test if your object ...READ MORE

answered Feb 28 in Python by SDeb
• 13,160 points
41 views
0 votes
0 answers

what is the purpose of the argument 'end' in the print function?

is it necessary to use this argument ...READ MORE

May 9 in Python by Waseem
• 4,480 points
14 views
0 votes
1 answer

What is the purpose of using lambda functions in Python?

The main purpose of anonymous functions come ...READ MORE

answered Jun 11 in Python by Nisa
• 1,090 points
37 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 216 views
+4 votes
6 answers
0 votes
1 answer

What is the output of and explanation?

It will print concatenated lists. Output would ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 56,520 points
24 views
0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 56,520 points
70 views