Python self Parameter 👈
The self Parameter
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
✨ Naming Convention
It does not have to be named self, you can call it whatever you like, but it has to be the first parameter of any function in the class:
python
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()💡 Did You Know?
Although you can name it anything, it is strongly recommended to use the name self to follow Python conventions and make your code readable for other developers.
✨ Pro Tip
When calling an instance method, Python automatically passes the instance as the first argument. That's why you include self in the definition but don't pass it when calling the method.