NEXT TUTORIAL >> self variable in Python
Classes and Objects in Python: We all know that Python is an Object oriented programming language as it works around different classes and objects that too with different behaviors. Each and everything in Python is considered to be an object with its own attributes and methods.
Similarly, we can say that a Python Class is a blueprint for creating objects.
Let’s dig in more deep with a real world example,
1. Consider ‘Animal’ as a class and ‘Animal Species’ (Dog, Cat, Bird) as its attributes.

Creating a Class in Python
We can create a class in Python with the keyword class followed by a classname.
General format for a Python class is given as :
class ClassName:
attribute 1
attribute n
def __init__(self):
statements
def method1():
statements
def method2():
statements
Above class ‘Animal‘ can be defined as :
class Animal:
def __init__(self):
self.dog = 'bark'
self.cat = 'meow'
self.bird = 'chirp'
def display(self):
print('Dog {}s'.format(self.dog))
print('Cat {}s'.format(self.cat))
print('Bird {}s'.format(self.bird))
Writing a class is not the only step that’s required, it should be used somewhere in the code.
To make that class functional, we should create an instance (object) of that class.
Syntax for creating an instance,
instance_name = Classname()
So, above code becomes,
class Animal:
def __init__(self):
self.dog = 'bark'
self.cat = 'meow'
self.bird = 'chirp'
def display(self):
print('Dog {}s'.format(self.dog))
print('Cat {}s'.format(self.cat))
print('Bird {}s'.format(self.bird))
a = Animal() # creating an instance of a class Animal
print('a.dog: ', a.dog) # printing attribute of a class Animal
print('a.cat: ', a.cat) # printing attribute of a class Animal
print('a.bird: ', a.bird) # printing attribute of a class Animal
a.display() # calling method of a class Animal
O/P

Let’s see what happened in above code
1. We have defined a class Animal – with three variables (instance variables) and one method.
2. We have also used __init__() method to initialize the value for the class.
3. a = Animal() : Here, ‘a’ is an instance name. Once this line is executed, a block of memory is allocated on heap depending upon the class attributes.
4. After memory allocation, the special method ‘__init__(self)‘ is called internally. This method is used to store the initial values into the variables.
5. Finally, allocated memory address of the instance is returned into ‘a’.
We can reference any variables or methods using dot operator as:
a.dog, a.cat, a.bird, a.display()
6. ‘self‘ is a default variable and a reference to the current instance of the class, and is used to access variables that belongs to the class. Use of name ‘self‘ is optional, you can name it as per your wish.
Few Facts about Python Class
1. It is suggested to start a Python Class with an ‘uppercase‘ letter. For example, ‘Animal’, ‘God’, ‘Test’, ‘Main’ are some good looking Python classes.
2. Python Class can contain other nested classes. Nested class is a class defined within a class.
3. Python Class can have two or more __init__() methods, in all such cases, only the last __init__() method is considered.
class Main:
def __init__(self):
print('hey')
def __init__(self):
print('hi')
def __init__(self):
print('hy')
Main()
O/P
hy
Please note that having more than one __init__() method inside a class is really a bad bad practice.
4. Python Class supports modern’s day programming paradigm – Object Oriented Programming principle.
5. It is possible to define relationships among different classes. It is the concept of Inheritance.
That’s all here.
NEXT TUTORIAL >> self variable in Python
