Python 3 – Object Oriented
Object-oriented programming (OOP) is a programming paradigm that utilizes objects to represent and interact with real-world entities. Python 3 is a multi-paradigm programming language that supports OOP. In this article, we will be exploring the basics of OOP in Python 3.
Classes and Objects
In Python 3, objects are instances of classes. Classes are essentially blueprints that define the attributes and methods of objects. Here’s a simple example of a class:
class Dog:
species = "Canis lupus familiaris"
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print("Woof!")
In this example, we define a Dog
class that has a species
attribute and two methods, __init__()
and bark()
. The __init__()
method is a constructor that takes two arguments, name
and age
, and initializes the name
and age
attributes of the object. The bark()
method simply prints “Woof!” to the console.
To create an object (or instance) of the Dog
class, we can do the following:
my_dog = Dog("Fido", 3)
print(my_dog.name) # Output: Fido
print(my_dog.species) # Output: Canis lupus familiaris
my_dog.bark() # Output: Woof!
In this example, we create an object of the Dog
class called my_dog
with the name “Fido” and age 3. We then access the name
and species
attributes of the object using dot notation, and call the bark()
method of the object.
Inheritance
Inheritance is a mechanism that allows a new class to be based on an existing class, inheriting all of its attributes and methods. In Python 3, we can create a new class that inherits from an existing class using the following syntax:
class Poodle(Dog):
def bark(self):
print("Yap!")
In this example, we define a Poodle
class that inherits from the Dog
class. The Poodle
class overrides the bark()
method of the Dog
class by printing “Yap!” instead of “Woof!”.
my_poodle = Poodle("Fluffy", 2)
print(my_poodle.name) # Output: Fluffy
print(my_poodle.species) # Output: Canis lupus familiaris
my_poodle.bark() # Output: Yap!
In this example, we create an object of the Poodle
class called my_poodle
with the name “Fluffy” and age 2. We then access the name
and species
attributes of the object using dot notation, and call the overridden bark()
method of the object.
Encapsulation
Encapsulation is a mechanism that allows data and behavior to be grouped together into a single unit, called a class. The internal workings of the class are hidden from the outside world, and can only be accessed through the class’s public interface.
In Python 3, encapsulation can be achieved by making class attributes and methods private using the double underscore (__
) prefix. Here’s an example:
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient funds")
def get_balance(self):
return self.__balance
In this example, we define a BankAccount
class that has a private __balance
attribute and three methods, __init__()
, deposit()
, and withdraw()
. The __init__()
method is a constructor that takes a balance
argument and initializes the private __balance
attribute of the object. The deposit()
and withdraw()
methods add and subtract from the private __balance
attribute, respectively. The get_balance()
method returns the private __balance
attribute.
my_account = BankAccount(1000)
my_account.deposit(500)
my_account.withdraw(2000)
print(my_account.get_balance()) # Output: 1500
In this example, we create an object of the BankAccount
class called my_account
with a balance of 1000. We then deposit 500 into the account and withdraw 2000 from the account, resulting in a balance of 1500. We retrieve the account balance using the get_balance()
method, which returns the private __balance
attribute.
Polymorphism
Polymorphism is a mechanism that allows objects of different classes to be used interchangeably, even though they may have different attributes and methods. In Python 3, polymorphism can be achieved through inheritance and method overloading.
Method overloading is a mechanism that allows a method to be defined with the same name but different parameters in different classes. Here’s an example:
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Square:
def __init__(self, side_length):
self.side_length = side_length
def area(self):
return self.side_length * self.side_length
In this example, we define a Circle
class and a Square
class, both of which have an area()
method. Although the methods have the same name, they are defined differently in each class, allowing them to be used interchangeably.
my_circle = Circle(5)
my_square = Square(5)
print(my_circle.area()) # Output: 78.5
print(my_square.area()) # Output: 25
In this example, we create an object of the Circle
class called my_circle
with a radius of 5, and an object of the Square
class called my_square
with a side length of 5. We then call the area()
method on both objects, which returns the area of the circle and square, respectively.
Conclusion
In conclusion, object-oriented programming is a powerful paradigm that can be utilized in Python 3 using classes and objects, inheritance, encapsulation, and polymorphism. By understanding and utilizing these concepts, you can create efficient and organized programs that are easily maintainable and scalable. So go forth and create amazing Python 3 programs with the power of OOP!