The __init__ method
In Exercise 72 – creating a Pet class, you used the Pet
class to create a Pet
object called chubbles
in the following manner:
chubbles = Pet()
Here, you’ll explore more about what happens when you create objects from a class in this manner.
Python has a special method called __init__
, which is called when you initialize an object from one of our class templates. For example, building on the previous exercise, suppose you wanted to specify the height of a pet. You would add an __init__
method as follows:
class Pet:
"""
A class to capture useful information regarding my pets, just in case
I lose track of them.
"""
def __init__(self, height):
self.height = height
is_human = False
...