How to Create Classes in Python [ Python OOP ]

In this Python OOP article i want to show you How to Create Classes in Python [ Python OOP ],

so first of all let’s talk what are python classes and after that we are going to create practical

example.

 

 

 

 

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 MyClass, 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.

 

 

 

 

How to Add Attributes in Python 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 you will see this result.

How to Create Classes in Python [ Python OOP ]
How to Create Classes in Python [ Python OOP ]

 

 

 

 

How to Create 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 email in the class.

 

 

Run the code and this is the result.

Python OOP
Python OOP

 

 

 

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

 

 

Python 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.

 

 

Python 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.

 

 

 

How to Add Method in Python Class

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

 

 

 

Leave a Comment