The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
It is how Python knows which specific object you are working with when you call a class method or modify an attribute!
self?No! It does not have to be named self. You can call it whatever you like, but it must be the first parameter of any function in the class.
Naming it self is a widely adopted Python convention, and sticking to it is highly recommended so other developers can easily read your code.
// Use the words 'mysillyobject' and 'abc' instead of 'self':
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Must the first parameter of a class method strictly be named self?