class Dog:
def __init__(self, name):
self.name = nameLine-by-line explanation
- The class definition begins.
__init__receives a new object and a name.self.nameis an attribute on that object.- The passed name is stored there.
def bark(self):
print("Woof!")Line-by-line explanation
- This indented definition belongs to Dog.
barkis a method.- Calling it displays Woof.
pet = Dog("Milo")
pet.bark()Line-by-line explanation
- A Dog object is created with the name Milo.
petstores it.- The bark method is called on that object.