Advanced Python Tutorials – Object-Oriented Programming

Advanced Python Tutorials – Object-Oriented Programming

We have understood important Python concepts like Iterators, Generators, and Decorators in Python. In this advanced Python tutorials, we are going to learn all about Object-Oriented Programming.

Welcome to the part-2 of the advance python series. In the first chapter, we have understood important python concepts like Iterators, Generators, and Decorators in Python. In this chapter, we are going to learn all about Object-Oriented Programming.

Object-Oriented Programming in Python

Object-Oriented programming is very important as in every programming language, it is essential to modularize the code so that it can be re-used. We don’t want to write similar code at multiple places and also we want to provide a proper structure to the code so that it’s easier to understand. Hence to solve these and many other problems, we use Object-Oriented Programming. It’s not specific to any language however it’s a programming paradigm which many languages follow.

Classes and Instance Variables

Why we should use Classes. Well, Classes are used in most modern programming languages. They provide a way to logically group data and functions. Data correspond to attributes or properties and functions correspond to a method.

Let’s see this with an example. Suppose you are part of a superhero company (like Vought from the Boys tv series) and want to build an application for your company where you need to keep the track of all the superheroes. Hence to represent a Super Hero, you can use class for it.

class Heroes:
    pass

hero_1 = Heroes()
hero_2 = Heroes()

hero_1.first = "Home"
hero_1.last = "Lander"
hero_1.salary = 50000

hero_2.first = "Black"
hero_2.last = "Noir"
hero_2.salary = 70000

print(emp_1.first)
print(emp_2.first)
Home
Black

You can define a class with the class keyword. In the above example, I have not provided any attributes or methods just used the pass keyword which means the class is not doing anything. however, I am creating 2 instances of the class — emp_1 and emp_2. Instance variables contain data that is unique to each instance. Here I am manually defining the instance variable so if I have to do this for all the employees in the organization, it’s very painful and we don’t exactly get the benefit of the class.

So now we have to improve our code. Let’s redefine the class and create the same 2 employees.

class Heroes:
    def __init__(self, first, last, salary):
        self.first = first
        self.last = last
        self.salary = salary
        self.email = first + "." + last + "@vought.com"

    def getEmail(self):
        return self.email

hero_1 = Heroes("Home","Lander", 50000)
hero_2 = Heroes("Black", "Noir", 70000)

print(hero_1.salary)
print(hero_1.getEmail())
print(hero_2.getEmail())

print("\nAnother way of getting hero_1 name")

print(Heroes.getEmail(hero_1))
50000
[email protected]
[email protected]

Another way of getting hero_1 name
[email protected]

This looks better as you have created a class blueprint and you can create any number of class objects i.e. employees. Let’s understand a few points here.

  1. _init _is a special method of the class which is also known as the Constructor in other programming languages. We use these special methods to define an object or instantiate an object. This basically tells us how objects are different from each other.
  2. self means the instance itself. When we create an object, it passes the instance variable. You can provide any name for the instance variable but by convention everyone uses self.
  3. You can also call a method using the class name but in that case, you also have to provide the instance name (Employee.getemail(emp_1)). emp_1 is the instance variable that will be referred to as self inside the class.

Class Variables

Class variables are the variables that are shared amongst all instances of the class. Instance variables are unique to each instance however class variables will share the same value for all the instances.

Let’s understand this with a use case. Suppose you want to keep track of the number of employees in the organization. The number of employees can be a variable for which value will be the same across all the employees so we can define this as a class variable.

class Heroes:
    No_Of_Employees = 0

    def __init__(self, first, last, salary):
        self.first = first
        self.last = last
        self.salary = salary
        self.email = first + "." + last + "@vought.com"

        Heroes.No_Of_Employees += 1

    def getEmail(self):
        return self.email

print("No of Employees before: ", Heroes.No_Of_Employees)
hero_1 = Heroes("Home","Lander", 50000)
hero_2 = Heroes("Black", "Noir", 70000)

print("No of Employees after: ", Heroes.No_Of_Employees)
No of Employees before:  0
No of Employees after:  2

You can also use the self keyword with the class variables instead of the class name. There could be a scenario where you have defined a class variable with a particular value which will be the same for most of the instances but for a particular instance, you need to provide a different value. In that case, it’s better to use self.class_valiable_name instead of class.class_variable_name in the *init *method and override the value with instance_name.class_variable_name.

So whenever you call a class variable with the instance name, first it checks for that variable in the instance variables, and if not found then check for the class level variables.

python oop web-development data-science developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

Applied Data Science with Python Certification Training Course -IgmGuru

Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...

Data Science With Python | Python For Data Science | Data Science For Beginners

This Data Science with Python Tutorial will help you understand what is Data Science, basics of Python for data analysis, why learn Python, how to install Python, Python libraries for data analysis, exploratory analysis using Pandas, introduction to series and dataframe, loan prediction problem, data wrangling using Pandas, building a predictive model using Scikit-Learn and implementing logistic regression model using Python.