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


Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found