how do I post a python FAQ? I can’t find it in the categories.
also: why do methods in classes always have to have a parameter self
how do I post a python FAQ? I can’t find it in the categories.
also: why do methods in classes always have to have a parameter self
self
is the current instance of the class, which we can show with the following example:
class MyClass():
def hello_world(self):
print('hello world')
instance = MyClass()
instance.hello_world()
MyClass.hello_world(instance)
when calling a method on class, python will handle the self
parameter
this line:
MyClass.hello_world(instance)
you can see instance
being passed to the self
parameter (because the method is called on class, not on instance)