Brief Introduction to OOP in Python

Brief Introduction to OOP in Python

Object-oriented programming is a programming paradigm for solving programming problems.

·

4 min read

Table Content

  • What Is Object-Oriented Programming?
  • What is mean by Class and Object?
  • How to define a class in Python?
  • Initiating objects in Python.
  • Class Methods
  • Conclusion

What is Object-Oriented Programming?

Object-oriented programming is a programming paradigm for solving programming problems. Object-oriented programming is all about creating and manipulating objects.

For example, an object could be a dog with properties like name, age, breed, and behaviors such as barking, playing, running. Or it can be Car with properties like model name, company, horsepower, and behaviors like accelerating, speed, gear changing.

Object-oriented programming is famous because it implements real-world entities like a Person, Car, Dog, Bank etc in programming. The concept of OOP focuses on creating reusable code. This concept is also known as DRY(Don't Repeat Yourself). The topic for another article may be. Let's focus on OOP now.

The key takeaway here is, objects are the center of object-oriented programming.

What is mean by Class and Object?

A class is a data structure or blueprint for the object. There are primitive data structures like int, string, and list are designed to store simple data. These primitive data structures come out of the box with Python. But the class data structure is user define.

A blueprint means how something should be defined. It doesn't actually have data. Suppose we have created a class Bank Account. The Bank Account class specifies the name, account number, balance, account type of the person. But, it does not contain the name, account number, the balance of the specific person.

The object of that class has all the values for the specific person.

How to define a class in Python?

A class in python is defined by class keyword, which is followed by the name of the class.

Here's is an example of a Bank Account class:

class BankAccount:
    pass

This is a simple class that does not have any properties and behaviors. The properties that all Bank Account should have are defined in a method called __init__(). Every time the Bank Account object is created __init__() sets the initial state of the object.

You can give any number of parameters to the __init__() method or we can call it as Constructor. But, the first parameter to the constructor should always be a Self keyword.

Take a look at our updated Bank Account class:

class BankAccount:
    def __init__(self, name, account_type, mobile_number, balance):
        self.name = name
        self.account_type = account_type
        self.mobile_number = mobile_number
        self.balance = balance

In the above code snippet, __init__() method uses the self keyword.

self.name = name creates the attribute called name and assigns it to the value of the name parameter. And, following statements are the same as above with different values.

####Initiating Objects in python

Creating a new object from a class is called initiating an object.

userOne = BankAccount('James', 'Saving', 1234567890, 2000)

This creates a new instance for the Bank Account named userOne and assigns James as name, Account type as saving, mobile number as 1234567890, and balance as 2000.

You can access their instance attributes using the dot.

print(userOne.name)
-> James

Print(userOne.account_type)
-> Saving

####Class Methods:

Class methods are functions defined inside a class and can only be called using instances of that class. Every method in the class taking Self as the first parameter. Except get methods.

Here's is an example of a class method in our Bank Account Class.

class BankAccount:
    def __init__(self, name, account_type, mobile_number, balance):
        self.name = name
        self.account_type = account_type
        self.mobile_number = mobile_number
        self.initial_balance = initial_balance

    def deposit(self, amount):
        self.balance += amount

    def getBalance():
        return self.balance

Our BankAccount class has two methods deposit and getBalance. deposit method takes one parameter amount. For example, How much amount you want to deposit in your bank. You can call the deposit method as follows.

james = BankAccount('James', 'Saving', 1234567890, 2000)

#call deposit method using an instance of BankAccount
james.deposit(5000)

In the above code, first, we have created an instance of BankAccount name as James. James deposited 2000 as the initial balance. After we have called class method deposit() and deposited 5000.

Let's now fetch the balance for user James.

james.getBalance()
-> 7000

The getBalance() method will return the balance of 7000. Because at first balance was 200 and then we deposited 5000. 2000 + 5000 = 7000, hence 7000 is returned.

Conclusion:

In this simple and basic introduction to Object-Oriented Programming in Python, we have learned. OOP center around objects. Class is a blueprint to create and manipulate objects. Objects are nothing but real-world entities. Constructor __init__() is called, when we create the instance of the class. Class methods take Self as the first parameter.