Let's say that I have a tiny code like this:
#!/usr/bin/env python3
class Dog():
def __init__(self, name, age):
self.name = name
self.age = age
def sit(self):
print(self.name + " is now sitting.")
my_dog = Dog('willie',6)
my_dog.sit()
As I understand, the line my_dog = Dog('willie',6)
creates an instance named my_dog
. It calls the __init__
function, converts it internally to Dog.__init__(my_dog, willie, 6)
and sets the my_dog.name
and my_dog.age
variables to willie
and 6
? Now when the my_dog.sit()
is executed, then why does the method sit()
require a self
as a parameter? Is it because if there is another instance of the same class(for example, your_dog
), then it knows to use your_dog.name
instead of my_dog.name
?