Classes in Python

Rishav Anurag
2 min readJul 6, 2024

--

Class is the logical representation of an entity or a real-world thing, like Human, Utensil, Vehicle, Animal, etc. Classes encapsulate the properties and behavior of an entity. The word encapsulation fits perfectly as the class keeps all the ingredients of an entity like a medical capsule keeps all the enzymes inside it.

Classes provide abstraction, which means that the outer world can not directly see how any behavior is defined in the class.

To define a class in Python ‘class’ keyword is used before the class name(unlike variables the naming convention used for the class name is camel case) and all the details of the class are encapsulated within the class block

class Human:
# properties/ class variables
creature_name: "creature1"
creature_address: "address1"

# class behavior/function/method
def walk(self):
print("Walks on two feets")

def speak(self):
print("Speaks human language")

#continued

Objects

Objects are the physical representation of the class. In other words, we can say that an object establishes an “IS A” relationship with the class.
For example, we can say that a programmer p1(object of class Human) is a human(class)

To create an object of a class, we assign a class name along with parentheses to a variable

p1 = Human()

To access the variable/property and behavior/function of a class, ‘.’ is used with the object name

print(p1.creature_name) # creature1
print(p1.creature_address) # address1

Constructor

Constructor is the function/method, which gets executed the very first time, whenever an object is created at runtime. Constructor is ideally used to initialize the initial properties of the object, and this function does not return any data. In Python, the constructor function is always created with the name __init__(self), where self is the default first positional argument.

Self

Self represents the instance of the class. By using the “self” we can access the attributes and methods of the class in Python. It binds the attribute with the instance of the class.
In other words, it is a keyword that points to the current passed instance. But it need not be passed every time while calling an instance method

Like in the previous example, we have created a Human class having attributes/properties like name creature_name and creature_address which is hardcoded as of now, so all the objects created will have the same creature_name and creature_address. To solve this issue, a constructor was introduced to set the creature_name and creature_address each time an instance of the class is created.

class Human:
# properties
def __init__(self, name, address): # constructor
self.creature_name = name
self.creature_address = address

# behavior/function/method
def walk(self):
print(f"{self.creature_name} walks on two feet")

def speak(self):
print(f"{self.creature_name} speaks human language")


h1 = Human("Rishav", "Bengaluru")
h2 = Human("Swati", "Kolkata")

print(h1.creature_address) # Bengaluru
print(h1.creature_name) # Rishav
print(h1.walk()) # Rishav walks on two feet

print(h2.creature_address) # Kolkata
print(h2.creature_name) # Swati
print(h2.speak()) # Swati speaks human language

--

--

Rishav Anurag

Senior Analyst@Accenture, Ex-Software Developer@Ericsson, Ex-Digital@TCS