Does the first parameter to a class method have to be named self?


#1

Question

Does the first parameter to a class method always need to be named self?

Answer

self is not a reserved or special keyword in Python. It is, however, a strongly encouraged practice to always name the variable referencing the current object as self. This will make your code easier to understand by other people as it will follow the common naming convention.


#2
>>> class foo(object):
	def __init__(this, name, age):
		this.name = name
		this.age = age

		
>>> bar = foo("Bar", 63)
>>> bar
<__main__.foo object at 0x00F0FC10>
>>> bar.name
'Bar'
>>> bar.age
63
>>> 

Why would we opt for bucking the recommended diction? The syntax highlighting is very supportive of this, no pun intended, supportive of self is more the point.