The __init__ method
In Exercise 71, Creating a Pet Class, you used our 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 our Pet
. We would add an __init__
method as follows:
class Pet(): """ A class to capture useful information regarding my pets, just incase I lose track of them. """ def __init__(self, height): self.height = height is_human = False ...