Python Tutorial
About Lesson

In this Python Object Oriented Programming(OOP) lesson we want to learn about Python Classes, in this lesson we will cover different topics like creating classes, creating instances, how you can add methods and attributes to the classes and what are difference between class variable and instance variable.

 

 

What is Object Oriented Programming(OOP)

As you know Python is object oriented programming language, it means that we can solve a problem in Python language by creating classes and objects in our program. so we can say that object oriented programming is a computer programming model that arranges software design with data, or objects, rather than functions. there are different concepts that you should know in object oriented programming like creating classes, creating object,  and there are some object oriented principles like inheritance, polymorphism, abstraction and encapsulation, particularly in this video we want to learn how you can create classes and how you can instantiate a class in Python.

 

 

 

What are Python Classes ?

A class is a blueprint for the objects. or we can say that a class is a way of organizing information about a type of data, by creating a class we can reuse elements when making multiple instances of that data type for example, we create a class Car, now if we want to create the instances of this car class, it may be a BMW car or Ferrari car or another type of car. the Car class would allow you to store similar information that is unique to each car (they are different models, and maybe different colors, etc.) and associate the appropriate information with each car.

 

 

So now let’s create a simple class, you can use class keyword followed by the name of the class in Python. in the example we have created a simple class at name of ClassName, right now we are not adding anything the class body.

 

 

If you want to use this class you need to instantiate the objects from this class. creating of instance class is simple, you can just give the name of the class followed by pair of parenthesis.

 

 

Adding Attributes to Class 

right now we are not going to do anything in the class body itself,  we want to add some attributes to a created object of the class. we have created a simple class, and after that we have instantiated two objects from the class, after that we have assigned some attributes to the class using dot notation.

 

 

 

Run the code and this is the result.

Python Classes - Python Object Oriented Programming(OOP)
Python Classes – Python Object Oriented Programming(OOP)

 

 

 

Python Initializer Method 

Most object oriented programming languages have the concept of a constructor, a special method that creates and initializes the object when it is created, now in python we have initializer method this is a special method and it is called when an object is created from a class and it allows the class to initialize the attributes of the class.

As you can see in the above example we have created an initializer method, and the self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python, when you want to create the instance of the class you need to pass the name and age in the class.

 

Python object has two types of attributes: Class Attribute and Instance Attribute.

 

 

Class Attribute 

A class attribute is a Python variable that is owned by a class rather than a particular object. It is shared between all the objects of this class and it is defined outside the initializer method of the class.

 

 

Instance Attribute 

An instance attribute is a Python variable belonging to only one object. This variable is only accessible in the scope of this object and it is defined inside the init() method also Instance Attributes are unique to each object.

 

 

Now let’s take a look at this example, in here the email is a class attribute and it belongs to the class, it is shared by all instances. When you change the value of a class attribute, it will affect all instances that share the same exact value. the variable name and age are instance variable, because they are unique to the instance, if you change one instance variable, the second instance variable will not be affected. but in class attribute when you change the variable, it will affect all the instances.

 

 

 

Adding Method in the Class 

Now let’s add method in the class, take a look at this example.

 

 

 

If you run the code this will be the result.

Python Class Methods
Python Class Methods

 

 

 

Benefits of Python OOP

Python is an object oriented programming language it means that it provides different features and benefits that make it well suited for object oriented programming (OOP). these are some of the main benefits of using OOP in Python:

  1. Encapsulation: Python allows you to group related data and functions together into single unit, called class. this makes it easier to manage and maintain your code, and it also helps to prevent conflicts and errors that can arise when different parts of the program interact with the same data in different ways.
  2. Inheritance: Inheritance is key feature of OOP that allows you to create new classes based on existing ones. this makes it easy to reuse code and to create specialized classes that share common attributes and behaviors. in Python you can use inheritance to create new classes that inherit the properties and methods of existing ones.
  3. Polymorphism: Polymorphism is the ability of objects to take on different forms, depending on the context in which they are used. in Python you can use polymorphism to create classes that behave differently depending on the context in which they are used. for example you could create class called Shape that has a method called area(). you could then create subclasses of Shape for example Circle and Rectangle each of which implements the area() method in different way.
  4. Modularity: OOP makes it easy to create modular and reusable code that can be used in different parts of program. by breaking down complex problems into smalle more manageable pieces, you can create classes and objects that can be reused in different parts of the program.
  5. Code organization: OOP provides a way to organize your code into logical units that make it easier to understand and maintain. by grouping related functions and data into classes, you can create a clear and understandable structure for your code.

 

Overall, OOP provides number of benefits that make it popular and powerful approach to programming in Python. by using classes, inheritance, polymorphism, modularity and good code organization, you can create code that is more modular, reusable and maintainable than code written using other programming paradigms.