Skip to content

Learning Python: Day 24

The __init__() method

  • method – function that’s part of a class
  • rules that apply to functions also apply to methods
  • __init__() method – is a special method that Python runs automatically whenever we create a new instance based on the Dog class
  • this method has two leading and trailing underscores, which prevents Python’s default method names from conflicting with your method names
  • __init__() method is defined to have three parameters: self, name, and age
  • self parameter is required in the method definition, must come first
  • must be in the definition because when Python calls this method later (to create an instance for Dog), the method call will automatically pass the self argument
  • whenever we want to make an instance from the Dog class, we’ll provide values for only the last two parameters, name and age since self is passed automatically
  • two variables defined in the body of the __init__() method each have the prefix self
  • any variable prefixed with self is available to every method in the class
  • we’ll also be able to access these variables through any instance created from the class
  • line self.name = name takes the value associated with the parameter name and assigns it to the variable name, which is then attached to the created instance
  • attributes – variables that are accessible through instances
  • Dog class has two other methods defined: sit() and roll_over() which don’t need additional information to run, we define them to have one parameter, self
  • instances created later can access these methods, which means they can sit and roll over
  • for now, these methods don’t do much except print a message that the dog is sitting or rolling over
  • concept can be extended to real-life scenarios
  • in a video game, these methods would contain code to make an animated dog sit and roll over, if this class was written to control a robot, these methods would direct movements that cause a robotic dog to sit and roll over

Making an instance from a class

  • class is like a set of instructions for how to make an instance
  • Dog class is a set of instructions that tells Python how to make individual instances representing specific dogs

class Dog:

….–snip–

my_dog = Dog(‘Willie’, 6)

print(f”My dog’s name is {my_dog.name}.”)

print(f”My dog is {my_dog.age} years old.”)

  • we tell Python to create a dog whose name is ‘Willie’ and whose age is 6
  • this line tells Python to call the __init__() method in Dog with the arguments ‘Willie’ and 6
  • __init__() method creates an instance representing this particular dog and sets the name and age attributes using the provided values
  • instance assigned to the variable my_dog
  • a capitalized name like Dog refers to a class, and a lowercase name like my_dog refers to a single instance created from a class

Accessing attributes

  • use dot notation to access the attributes of an instance
  • this is how to access the value of my_dog’s attribute name

my_dog.name

  • syntax demonstrates how Python finds an attribute’s value
  • Python looks at the instance my_dog and then finds the attribute name associated with my_dog
  • same attribute referred to as self.name in the class Dog
  • use same approach to work with the attribute age

my_dog:

My dog’s name is Willie.

My dog is 6 years old.

Calling Methods

  • after creating an instance from the class Dog, we can use dot notation to call any method defined in Dog

class Dog:

….–snip–

my_dog = Dog(‘Willie’, 6)

my_dog.sit()

my_dog.roll_over()

  • to call a method, give the name of the instance (in this case, my_dog) and the method you want to call, separated by a dot
  • when Python reads my_dog.sit(), it looks for the method sit() in the class Dog and runs the code

Willie is now sitting.

Willie rolled over!

  • useful when attributes and methods are given descriptive names to understand what a block of code is saying

Creating multiple instances

  • can create as many instances form a class as needed
  • let’s create a second dog called your_dog

class Dog:

….–snip–

my_dog = Dog(‘Willie’, 6)

your_dog = Dog(‘Lucy’, 3)

print(f”My dog’s name is {my_dog.name}.”)

print(f”My dog is {my_dog.age} years old.”)

my_dog.sit()

print(f”\nYour dog’s name is {your_dog.name}.”)

print(f”Your dog is {your_dog.age} years old.”)

your_dog.sit()

  • created a dog named Willie and a dog named Lucy
  • each dog is a separate instance with its own set of attributes, capable of the same set of actions

My dog’s name is Willie.

My dog is 6 year’s old.

Willie is now sitting.

Your dog’s name is Lucy.

Your dog is 3 years old.

Lucy is now sitting.

  • can create as many instances from one class as you need, as long as each instance is given a unique variable name or occupies a unique spot in a list or dictionary

End of study session.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *