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.
1 2 |
class MyClass: pass |
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.
1 2 3 4 5 6 |
class MyClass: pass a = MyClass() b = MyClass() |
- TKinter Tutorial For Beginners
- PyQt5 Tutorial – Build GUI in Python with PyQt5
- Python Speech Recognition For Beginners
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Student: pass st1 = Student() st2= Student() st1.name = "Parwiz" st1.age = 25 st2.name = "Geekscoders" st2.age = 20 print(st1.name, st1.age) print(st2.name, st2.age) |
Run the code and you will see this result.
![How to Create Classes in Python [ Python OOP ]](https://geekscoders.com/wp-content/uploads/2020/11/how-to-create-class-in-python.jpg)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Student: def __init__(self, name, email): self.name = name self.email = email st1 = Student("Geekscoders", 'geekscoders@gmail.com') st2 = Student('Parwiz', 'par@gmail.com') print(st1.name) print(st1.email) print(st2.name) print(st2.email) |
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 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Student: email = 'geekscoders@gmail.com' def __init__(self, name, age): self.name = name self.age = age st1 = Student("Geekscoders", 20) st2 = Student('Parwiz', 25) print(st1.name) print(st1.age) print(st1.email) print(st2.name) print(st2.age) print(st2.email) |
How to Add Method in Python Class
Now let’s add method in the class, take a look at this example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Student: def __init__(self, name, age, email): self.name = name self.age = age self.email = email def print_info(self): print(f"My name is {self.name} My Age is " f"{self.age} and My email is {self.email} ") st1 = Student("GeeksCoders", 20, "geekscoders@gmail.com") st1.print_info() |