Skip to main content

Python Class and Objects

Python also supports the concept of objects and classes.

In Python, everything is an object and every object has a state and behaviours.

An object is simply a collection of data (variables) and methods (functions):

  • The data (known also as state) represents the object in a particular moment in time. Attributes are used to model the state of an object.
  • The behaviours of an object are modelled using functions. When a function is associated with an object, it is called method of the object.

To create an object you have to:

  1. Define a class
  2. Create an object from that class, i.e. create an instance from that class

Python Class

A class in Python is a blueprint for creating objects: it is a way to define a new data type that can have attributes (variables) and methods (functions).

A class is created by using the class keyword followed by the name of the class.

Inside the class, you can define attributes and methods:

  • Attributes are variables inside a class that hold data.
  • Methods are functions that perform actions on the data.

For example:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def say_hello(self):
print("Hello, my name is", self.name)

In this example, we define a class called Person. It has two attributes: name and age. The __init__ method is a special method called the constructor, which is used to initialize the attributes of the class. The say_hello method is a simple method that prints a greeting message.

Python Class Constructor

A class constructor is a special method that is automatically called when an object (instance) of a class is created.

It is used to initialize the attributes of the object.

The constructor method is defined with the name __init__() and it takes the self parameter as the first parameter (which refers to the instance of the class being created) and other additional, but optional, parameters that can be used to initialize the attributes of the object.

For example, consider the constructor of the following class Person:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

We have the class Person with two attributes (name and age) and the __init__() method.

To create an object (instance) of the Person class and initialize its attributes, you can do as follows:

person1 = Person("Alice", 25)
person2 = Person("Bob", 30)

Note that person1 and person2 are two instances of the Person class. When these instances are created, the __init__() method is automatically called with the provided arguments.

warning

Constructor method is optional in a class.

If you don't define a constructor, Python provides a default constructor that doesn't perform any initialization!

Python Objects

An object is an instance of a class.

The syntax to create an object is:

objectName = ClassName(arg1, arg2, ...)

You just need to call the class as if it were a function, passing any required arguments to the constructor. For example:

person1 = Person("Alice", 25)
person2 = Person("Bob", 30)

In this case, person1 and person2 are two instances of the Person class, each with their own name and age attributes.

Another example with a simpler Person class:

# create class
class Person:
name = ""
age = 24

# create objects of class
person1 = Person()

Note that person1 is the object of the class.

Now, we can use this object to access the class attributes.

Access Class Attributes Using Objects

We use the . notation to access the attributes of a class.

For example:

# modify the "name" attribute
person.name = "Tom"

# access the "age" attribute
person.age

A complete example

Consider the following Person class with two attributes name and age. We then create an object of that class and access attributes using the . notation.

# create class
class Person:
name = ""
age = 0

# create object of Person class
person1 = Person()

# access attributes and assign new values
person1.name = "Tom"
person1.age = 24

print(f"Name: {person1.name}, Age: {person1.age} ")

Output:

Name: Tom, Age: 30 
note

We can also create multiple objects from a single class.

Just create multiple instances of the same class. For example:


# create object of Person class
person1 = Person()
person2 = Person()
person3 = Person()

Python Class is an Object too!

Remember that everything in Python is an object!

So, also classes are objects!

Python Methods

In Python, a method is a function that is defined inside a class and is associated with an object (instance) of that class. Methods are used to perform actions or operations on the data (attributes) of an object.

Methods are similar to functions, but they are defined within a class, they are accessed through instances of the class using the . operator and they have access to the attributes and other methods of the class.

For example:

class Circle:
def __init__(self, radius):
self.radius = radius

def calculate_area(self):
area = 3.14 * self.radius ** 2
return area

In this example, we have a class called Circle with an attribute radius and a method calculate_area(). The __init__() method is the constructor that initializes the radius attribute when a new instance of the class is created.

The calculate_area() method calculates the area of the circle using the formula 3.14 * radius^2. It uses the self parameter to access the radius attribute of the object.

To use the calculate_area() method, we need to create an instance of the Circle class:

circle1 = Circle(5)
area = circle1.calculate_area()
print(area) # Output: 78.5
note

Methods can also take additional parameters, just like regular functions.

The first parameter of a method is always self, which refers to the instance of the class and that is automatically passed when the method is called on an object.