Classes in Python
Classes in Python
They not only help us categorise objects, but also allow us to create objects. Classes are like
blueprints for creating objects.
For example, if we want to save products, we can create a class called Product:
class Product:
quantity = 200
product1 = Product()
print(product1)
# When we run this, we get the product object. Instead of printing the object itself, we can print
one of its attributes:
print(product1.quantity)
# Here we have created a class, created a class attribute, and printed it. The class attribute gets
assigned to all objects that we create from that class.
product2 = Product()
print(product2.quantity)
# It has the same quantity. But what about properties like price, product name, etc.? We want
different names and prices for different products.
Instance attributes cannot be created directly inside a class because the value of that attribute
gets assigned to all objects which are created from that class.
A constructor is a method that is automatically executed when we create an object of that class.
It is like a function definition.
class Product:
quantity = 200
# We will learn about self later. Self means a reference to that particular
# object itself.
# The following line sets this object's name to the name argument
# that is passed.
self.name = name
self.price = price
#product1 = Product()
#print(product1)
# This will give an error because we have not passed the arguments for the
# which means when we create an object, we need to also pass in the name
print(product2.name)
print(product2.price)
Methods:
Now that we have a product, let's say we want to apply a summer discount to the prices.
But in OOP, we create methods instead of functions. Methods are similar to functions, but they
are declared inside a class because they belong to that class.
These methods are also called instance methods because they can modify the attributes of the
object.
For example, as the discount method only has to work with products, it makes sense to add it
inside the Product class. Let's create that discount method here:
class Product:
quantity = 200
self.name = name
self.price = price
print(product1.price)
product1.summer_discount(10)
print(product1.price)
This code creates a Product object and sets its initial price to 500. We then call
the summer_discount() method on the product1 object, passing in a 10% discount. The method
calculates the new price based on the discount and updates the price attribute of the object.
Finally, we print the new price to confirm that the discount has been applied.
Polymorphism:
Polymorphism simply means the ability to take multiple forms. A real-world example of
polymorphism would be a person.
When a person is at work, they are an employee, and at home, they are a family member.
Python is polymorphic, meaning that a single operator, function, or class method in Python can
have multiple uses.
For example, the + operator can be used to add two numbers as well as concatenate two strings:
print(10+20)
print('Hello'+'world')
Some functions in Python are polymorphic, meaning their behavior changes depending on the
data we pass to them.
For example, the len function can count the number of characters in a string or the number of
items in a list:
print(len('HelloWorld'))
print(len(['Apple','Mango','Banana']))
Method overriding:
In general terms, overriding means to use your authority to reject somebody’s decision or order,
to be more important than something.
For example, a manager can fire an employee, but a CEO can reject this decision.
If we create an object of the Fruit class and call the type() method, it will override the type()
method of the Food class.
class Food:
def type(self):
print("Food")
class Fruit(Food):
def type(self):
print('Fruit')
burger = Food()
print(burger.type())
apple = Fruit()
print(apple.type())
# The method in the Fruit class overrides the method in the Food class.
Operator overloading:
Operator overloading allows you to overload operators like + and - and change their behaviour.
class Point:
self.x = x
self.y = y
x = self.x + other.x
y = self.y + other.y
# Once we have the values of x and y we want to return them into a point
return Point(x, y)
def __str__(self):
point1 = Point(1, 2)
point2 = Point(3, 4)
print(point1 + point2)
Now the above code returns an object, so we need to define the format in which the result
should be returned.
def __str__(self):
class Point:
self.x = x
self.y = y
x = self.x + other.x
y = self.y + other.y
# Once we have the values of x and y we want to return them into a point
return Point(x, y)
def __str__(self):
point1 = Point(1, 2)
point2 = Point(3, 4)
print(point1 + point2)