Classes - Animals Use Case


In this tutorial you will learn by example how to use and implement classes in Python. We will implement a class that describes a generic animal.


Python Class Example - Animals

The concept of Object-Oriented Programming might be a bit complicated at first. However, when you get the hang of it, OOP becomes a second nature. The best way to get there is by practice!

Let's create a brand-new class - animals.

Example - Animal Class in Python

When designing a class there are two main things we have to think about - the attributes of the class and its methods:

  • Attributes - variables of the class
  • Methods - represent esnt the behaviour of the class

Animal Class - Attributes

In our example any animal has a name, an age and hunger level. These attributes describe any animal instance. We use only 3 attributes in order to keep it simple, you may think of many more attributes that can be relevant.

  1. The animal has an age.
  2. The animal has a name.
  3. The animal has a hunger level.

Animal Class - Methods

Our animal class has 2 methods (behaviours) - run and eat:

  1. A method that makes the animal run. When it runs, the hunger level rises.
  2. A method that makes the animal eat. When it eats, the hunger level shrinks.

Animal Class - Summary

Python Class Example - Animals - methods and attributes

Animal Class - Implementation

Let's implement the class:

#Animal class class Animal(object): def __init__(self, name, age, hunger_level=9): self.name = name self.age = age self._hunger_level = hunger_level def run(self): if self._hunger_level == 10: print("Animal is too hungry to run") else: self._hunger_level += 1 def eat(self): if self._hunger_level == 0: print("Animal can't eat") else: self._hunger_level -= 1 def say_age(self): print(self.age) def say_hunger(self): print(self.hunger_level) #Create an animal object my_animal = Animal("Apollo", 5) my_animal.say_age() my_animal.run() my_animal.run() my_animal.eat()

Internal Attributes

Sometimes we would like to hide an attribute from the user. The way to do it is by adding an underscore to the attribute. This is called an Internal Attribute. In the example above we don't want the user to bypass the eat() and run() methods by accessing the method _hunger_level. That's why we hide the attribute using an underscore (_hunger_level).

Class Objects (Instances)

An object is an instance of a class. Consider the animal class above. It describes an idea - how animals should behave and what traits they have. An animal object is an actual animal with its own name, age, etc. You can have many instances of the same class. Here is an example:

#Animal class - objects class Animal(object): def __init__(self, name, age, hunger_level=9): self.name = name self.age = age self._hunger_level = hunger_level def run(self): if self._hunger_level == 10: print("Animal is too hungry to run") else: self._hunger_level += 1 def eat(self): if self._hunger_level == 0: print("Animal can't eat") else: self._hunger_level -= 1 def say_age(self): print(self.age) def say_hunger(self): print(self.hunger_level) #Create an animal object animal_1 = Animal("Apollo", 5) animal_2 = Animal("Rex", 3) animal_1.say_age() animal_2.say_age()

self

The self keyword represents the current instance of the class. Using this keyword, you can access the attributes of the instance. Moreover, class methods have the self as an extra first parameter.

__init__ Method in Python

The __init__ method is the constructor of the class. It initializes the object. Code that is written in the __init__ is run when declaring an instance.

Python Inheritance - Example

Consider we want to create a new class - Cats. We can code the class from zero, however if you think about it, it shares a lot if features with general animals - cats and general animals have name, age and hunger etc. Inheritance is a powerful tool that lets us define a new class with modification to an existing class.

  • Child Class - is the new class that we create.
  • Base Class (or parent) - is the class from which the new class inherits.

In our example we want to add features that are unique to cats. For instance, we can add eye color as an attribute and meow as a method.

Animal Inheritance - Summary

Python Class Inheritance - Cats - methods and attributes

Animal Inheritance - Implementation

#Animal class class Animal(object): def __init__(self, name, age, hunger_level=9): self.name = name self.age = age self._hunger_level = hunger_level def run(self): if self._hunger_level == 10: print("Animal is too hungry to run") else: self._hunger_level += 1 def eat(self): if self._hunger_level == 0: print("Animal can't eat") else: self._hunger_level -= 1 def say_age(self): print(self.age) def say_hunger(self): print(self.hunger_level) # Cats class Cat(Animal): def __init__(self, eye_color, name, age, hunger_level=9): self.eye_color = eye_color Animal.__init__(self, name, age, hunger_level=9) def meow(self): print("Meow") #Create a cat my_cat = Cat("Apollo", 5, "blue") my_cat.meow()

Exercise

Implement an animal class with different attributes and methods

#Write your code here