Classes In Python
Class
is a very powerful feature of object-oriented programming. In Python, we first write a class
to mimic any real word scenario and then create object
based on this class
. In this way, when we create individual object
from the class
, each object
is automatically equipped with the general behavior of a class
; however, we can give each object
whatever unique features we desire.Making anobject
from aclass
is called instantiation, and we work with instances of a class.
Let’s write our first class,
Restaurant
with two parameters ( restaurant_name
and cuisine_type
) and three methods ( __init__()
, describe_restaurant
and open_restaurant
)Using this
class
, we can write any instance
with the access to these methods:class Restaurant():
""" A class to store two attributes of a restaurant and have two methods (behaviors)"""
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(f"{self.restaurant_name.title()} serves {self.cuisine_type.title()} food.")
def open_restaurant(self):
print(f"{self.restaurant_name.title()} is open now.")
- Method: a
function
that is part of aclass
is called method - Special Method:
__init__()
is a special method and it is required. Python runs this automatically whenever we create a newobject
based on theclass
. - Parameters of Special Method: the
__init__()
method have three parameters:self
,restaurant_name
andcuisine_type
. Theself
parameter is required in the method definition, and it must come first, before the other parameters - Attributes: any variable prefixed with self is called an attribute. Attributes are available to every method in the
class
, and we can also access these variables(attributes) through anyinstance
created from theclass
. - Other Defined Methods: the
Restaurant
class has two other methods:describe_restaurant
andopen_restaurant
Because these methods don’t need additional information (parameters), we just define them to have one parameter,self
.
# making instance,restaurant from the class,Restaurant
restaurant = Restaurant('arabian nights','middle eastern')
# printing attributes
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
# calling methods of the class
restaurant.describe_restaurant()
restaurant.open_restaurant()
arabian nights
middle eastern
Arabian Nights serves Middle Eastern food.
Arabian Nights is open now.
restaurant = Restaurant('arabian nights','middle eastern')
- this line tells Python to create an instance(object),
restaurant
whoserestaurant_name
value isarabian nights
andcuisine_type
ismiddle eastern
. When Python reads this line, it calls the__init__()
method inRestaurant
class with the argumentsarabian nights
andmiddle eastern
- the
__init__()
method creates aninstance
representing this particular restaurant and sets therestaurant_name
andcuisine_type
using the values provided - you can create as many instances from a class as you need.
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
To access these attributes value directly we use dot notation —
restaurant.restaurant_name
and restaurant.cuisine_type
The syntax is essentially instance_name.parameter_name
restaurant.describe_restaurant()
restaurant.open_restaurant()
We can also use dot notation to call any method defined in
Restaurant
— restaurant.describe_restaurant()
and restaurant.open_restaurant()
arabian nights
middle eastern
Arabian Nights serves Middle Eastern food.
Arabian Nights is open now.
In this section, we will learn how to set a default value for attributes and, three ways to modify attributes value. We will use the previous example of class
Restaurant
in this section.Everyattribute
in aclass
needs an initial value, even if that value is 0 or an empty string. We can specify this initial value in the body of the__init__()
method; *if we do set a default value for an attribute, we don’t have to include a parameter for that attribute in method definition *
Let’s add an attribute called
numbers_served
and set its default value to 0
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.numbers_served = 0
As we have specified the default value of
self.numbers_served
, we don’t need to mention the parameter numbers_served
in the __init__()
We are going to create an instance and print the (default) attribute value for
numbers_served
restaurant = Restaurant('atlas kitchen', 'chinese')
print(f"{restaurant.restaurant_name.title()} has served {restaurant.numbers_served} customers")
Atlas Kitchen has served 0 customers
We will use three methods to modify an attribute value:
The simplest way to modify the value of an attribute is to declare the new value in the instance of the class. Here, we will modify the default value of attribute
numbers_served
to 100restaurant = Restaurant('atlas kitchen', 'chinese')
# declaring new value to overwrite the default value
restaurant.numbers_served = 100
print(f"{restaurant.restaurant_name.title()} has served {restaurant.numbers_served} customers")
Atlas Kitchen has served 100 customers
Instead of modifying the attribute value directly, we can pass the new value to a method (
set_numbers_served
) that handles the updating internally.Note: In the following example, we could set the new value without using
if
statement, but that will make the code open to errors where new value can be lower than the old value of numbers_served
---
def set_numbers_served(self, numbers_served):
if numbers_served >= self.numbers_served:
self.numbers_served = numbers_served
print(f"{self.restaurant_name.title()} has served {self.numbers_served} customers")
else:
print("Numbers served can't be lower than previous value")
Then we call this new method:
restaurant = Restaurant('atlas kitchen', 'chinese')
print(f"{restaurant.restaurant_name.title()} has served {restaurant.numbers_served} customers")
# updating numbers_served by calling method, set_numbers_served
restaurant.set_numbers_served(10)
Atlas Kitchen has served 0 customers
Atlas Kitchen has served 10 customers
Sometimes we want to increment an attribute’s value by a certain amount rather than set an entirely new value. Here’s a method that allows us to pass this incremental amount and add that value to the existing value of
numbers_served
---
def increment_numbers_served(self, increment_numbers):
if increment_numbers >= 1:
self.numbers_served += increment_numbers
print(f"{self.restaurant_name.title()} has served {self.numbers_served} customers")
else:
print("You can't increment the value by negative number or zero")
Let’s make an instance and call this new method:
restaurant = Restaurant('atlas kitchen', 'chinese')
print(f"{restaurant.restaurant_name.title()} has served {restaurant.numbers_served} customers")
# incrementing numbers_served by calling method, increment_numbers_served
restaurant.increment_numbers_served(5)
restaurant.increment_numbers_served(3)
restaurant.increment_numbers_served(2)
Atlas Kitchen has served 0 customers
Atlas Kitchen has served 5 customers
Atlas Kitchen has served 8 customers
Atlas Kitchen has served 10 customers
- In this section we will study the parent-child class structure. If the class we are writing (child) is a specialized version of another class (parent) that we wrote before, we can use inheritance structure
- The original class is called the parent class (superclass), and the new class is the child class (subclass)
- The child class automatically inherits all attributes and methods from its parent class but at the same time, can define new attributes and methods of its own
- Rule 1: When we create a child class, the parent class must be part of the current file
- Rule 2: When we create a child class, the parent class must appear before the child class in the file
- Rule 3: The name of the parent class must be included in parentheses
()
in the definition of the child class
Let’s create a child class,
IceCreamStand
from parent class, Restaurant
# creating child class, IceCreamStand
class IceCreamStand(Restaurant):
""" defining the parameters needed for the child class"""
def __init__(self, restaurant_name, cuisine_type):
""" Initializing the attributes from the parent class """
super().__init__(restaurant_name, cuisine_type)
# declaring attributes specific to child class
self.flavors = []
# defining method specific to child class
def display_flavors(self):
""" take the list of flavors provided by user and print it """
print(f"{self.restaurant_name.title()} serves the ice cream in following flavors:")
for self.flavor in self.flavors:
print(self.flavor.title())
- we created the child class,
IceCreamStand
by providing the name of parent class,Restaurant
within its parenthesis —IceCreamStand(Restaurant):
- The
__init__()
method takes in the required parameters from the parent class,Restaurant
- The
super().__init__
function is a special function that helps Python make connections between the parent and child class. This line tells Python to call the__init__()
method in parent class, which gives child class all the attributes of its parent class - then, we defined the attribute
self.flavors
specific to child class. This supposed to belist
, so we gave it an empty list default value[]
- then, we defined a method,
display_flavors(self)
that is specific to child class. This method used theself.flavors
attribute and print the value of each item in the list
# creating instance of child class
icecreamstand_1 = IceCreamStand('dairy queen', 'ice cream')
# demo: child class inherits all methods from its parent class
icecreamstand_1.describe_restaurant()
# providing child class its specific attribute and calling its specific method
icecreamstand_1.flavors = ['vanilla', 'chocolate', 'strawberry']
icecreamstand_1.display_flavors()
Dairy Queen serves Ice Cream food.
Dairy Queen serves the ice cream in following flavors:
Vanilla
Chocolate
Strawberry
- we created an instance of child class (
IceCreamStand
),icecreamstand_1
Arguments provided aredairy queen
, andice cream
- then we call a method, which is defined in the parent class to demonstrate how the inheritance works
- then we provided the attribute (
icecreamstand_1.flavors
) that is specific to child class (IceCreamStand
) and call the method (icecreamstand_1.display_flavors()
)that is also specific to child class
We can overwrite any method from the parent class when creating a child class. To do this, we define a method in the child class with the same name as the method we want to overwrite from the parent class. Python intelligently uses the method that we defined in the child class.
When our program start becoming lengthier and messier, we can break it down into various classes, where an instance of class can become an attribute of another class. The example below will help understand this concept:
# class to show the admin previledges
class Privileges():
""" Print admin previledges"""
def __init__(self):
self.privileges = ['can add post', 'can delete post', 'can band user']
def show_privileges(self):
print("Admin has following privileges:")
for self.privilege in self.privileges:
print(self.privilege)
class User():
""" To display the general information of a user """
def __init__(self, first_name, last_name, gender, email_address):
self.first_name = first_name
self.last_name = last_name
self.gender = gender
self.email_address = email_address
# attribute = instance of the class
self.privileges = Privileges()
def greet_user(self):
print(f"Welcome to the website {self.first_name.title()} {self.last_name.title()}")
# making instance of 'User' class
admin_user = User('dexter', 'morgan', 'male', '[email protected]')
# calling method of 'User' class
admin_user.greet_user()
# calling method of 'Priviledges' class
admin_user.privileges.show_privileges()
- we first defined the class (
Privileges
) that will be used as an attribute in other class (User
)Privileges
class has only one method,show_priviledges
- then while defining the
User
class, we give it an attribute ofprivileges
that takes its value from instance ofPrivileges
class
self.privileges = Privileges()
- then we created an instance of
User
, namedadmin_user
and provided the arguments
admin_user = User('dexter', 'morgan', 'male', '[email protected]')
- finally we call the
show_privileges
method ofPrivilege
class, as an attribute ofUser
class
admin_user.privileges.show_privileges()
The method to import class(es) from modules is similar to what we have covered in the Part 7: Functions in Python
In theory, we can store as many classes as we need in a single module, but it is good is each class in a module are related
from module_name import class_name
The import statement tells Python to open the module and import the class,
class_name
.The syntax to create
instance
is same as if the class
is defined in the same fileinstance_name = class_name(argument1, argument2, ...)
You
import
multiple classes from a module by separating each class
with a commafrom module_name import class_name1, class_name2
The syntax to create
instance
is same as if the class
is defined in the same fileinstance_name = class_name1(argument1, argument2, ...)
We can also import an entire module and then access the
classes
you need using dot notation. Therefore, we need to use the module name whenever creating the instanceimport module_name
instance_name = module_name.class_name(argument1, argument2, ...)
We can import all classes using the syntax defined in section 4.3. Then why do we need to
import
all classes
from module using this method? Because this way, we don’t need to use the dot notation.from module_name import *
instance_name = class_name(argument1, argument2, ...)
ALERT: This method to import classes is not recommended
One module maybe dependent on other modules, therefore, at the start of file, we can import other modules and required classes, using syntax
# start of module_1
from module_name_2 import class_name_x
from module_name_6 import class_name_y
The syntax to create instance is same as if the class is defined in the same file
Last modified 4mo ago