Python Classes and Objects

In this tutorial, you will learn about the core functionality of Python classes and objects with the help of examples.

Python Classes and Objects

In Object-Oriented Programming (OOP), an object is simply a collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object.

We can think of a class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions, we build the house. This house is, thus, the object.

Just as multiple houses can be made from a house's blueprint, we can create many objects from a single class.

A class is like a house blueprint and the houses are like objects
Blueprint (class) and houses (objects)

An object is also called an instance of a class, and the process of creating this object is called instantiation.


Define a Class in Python

In Python, class definitions begin with the class keyword.

The first string inside the class is called docstring and has a brief description of the class. Although not mandatory, it is highly recommended to include a docstring inside your class.

Here is a simple class definition.

class MyNewClass:
    '''This is a docstring. I have created a new class'''
    pass

A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions.

There are also special attributes in it that begin with double underscores __. For example, __doc__ gives us the docstring of that class.

As soon as we define a class, a new class object is created with the same name. This class object allows us to access the different attributes as well as to instantiate new objects of that class.

class Person:
    "This is a person class"
    age = 10

    def greet(self):
        print('Hello')

# Output: 10
print(Person.age)

# Output: <function Person.greet>
print(Person.greet)

# Output: "This is a person class"
print(Person.__doc__)

Output

10
<function Person.greet at 0x7fc78c6e8160>
This is a person class

Create an Object in Python

We saw that the class object could be used to access different attributes.

It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.

harry = Person()

This will create a new object instance named harry. We can access the attributes of objects using the object name prefix. For example,

# access method of harry object
harry.greet()

Attributes may be data or methods. Methods of an object are corresponding functions of that class.

This means that, since Person.greet is a function object (attribute of class), Person.greet will thus be a method object.

class Person:
    "This is a person class"
    age = 10

    def greet(self):
        print('Hello')

# create a new object of Person class
harry = Person()

# Output: <function Person.greet>
print(Person.greet)

# Output: <bound method Person.greet of <__main__.Person object>>
print(harry.greet)

# calling object's greet() method
# Output: Hello
harry.greet()

Output

<function Person.greet at 0x7fd288e4e160>
<bound method Person.greet of <__main__.Person object at 0x7fd288e9fa30>>
Hello

You may have noticed the self parameter in the method definition inside the class.

def greet(self):
    print('Hello')

However, we called the method simply by writing harry.greet() without any arguments, and it still worked.

This is because, whenever an object calls its method, the object itself is passed as the first argument. So, harry.greet() translates into Person.greet(harry).

Note: The first argument of the function in class must be the object itself. This is conventionally called self. It can be named otherwise, but we highly recommend you follow the convention.


Constructors in Python (init Function)

Class functions that begin with double underscore __ are called special functions as they have special meaning.

The __init__() function is a special function that gets called whenever a new object of that class is instantiated.

This type of function is also called a constructor in OOP. We normally use it to initialize all the variables.

class Complex_Number:
    def __init__(self, r=0, i=0):
        self.real = r
        self.imag = i

    def get_data(self):
        print(f'{self.real}+{self.imag}j')


# create a new Complex_Number object
num1 = Complex_Number(2, 3)

# call get_data() method
# Output: 2+3j
num1.get_data()

# create another Complex_Number object
# and create a new attribute 'attr'
num2 = Complex_Number(5)
num2.attr = 10

# Output: (5, 0, 10)
print((num2.real, num2.imag, num2.attr))

# but c1 object doesn't have attribute 'attr'
# AttributeError: 'Complex_Number' object has no attribute 'attr'
print(num1.attr)

Output

2+3j
(5, 0, 10)
Traceback (most recent call last):
  File "<string>", line 27, in <module>
    print(num1.attr)
AttributeError: 'Complex_Number' object has no attribute 'attr'

In the above example, we defined a new class to represent complex numbers. It has two functions, __init__() to initialize the variables (defaults to zero) and get_data() to display the number properly.

An interesting thing to note in the above step is that attributes of an object can be created on the fly.

We created a new attribute attr for object num2 and read it as well. But this does not create that attribute for object num1.


Delete Attributes and Objects

You can delete any attribute of an object anytime by using the del statement. Let's see an example below:

class Complex_Number:
    def __init__(self, realpart, imagpart):
        self.real = realpart
        self.imag = imagpart

    def get_data(self):
        print(f"{self.real}+{self.imag}j")

# create a new Complex_Number object
num1 = Complex_Number(2,3)

# delete the 'imag' attribute from num1
del num1.imag

# trying to access deleted attribute will raise an AttributeError
try:
    num1.get_data()
except AttributeError:
    print("'Complex_Number' object has no attribute 'imag'")

# delete the 'get_data' method from Complex_Number class
del Complex_Number.get_data

# trying to call deleted method will raise an AttributeError
try:
    num1.get_data()
except AttributeError:
    print("'Complex_Number' object has no attribute 'get_data'")

# create a new Complex_Number object
c1 = Complex_Number(1,3)

Output

'Complex_Number' object has no attribute 'imag'
'Complex_Number' object has no attribute 'get_data'

We can even delete the object itself, using the del statement as:

# create a new Complex_Number object
c1 = Complex_Number(1,3)

# delete the 'c1' object
del c1

# trying to reference deleted object will raise a NameError
try:
    print(c1)
except NameError:
    print("name 'c1' is not defined")

# Output: name 'c1' is not defined

Actually, it is more complicated than that. When we do c1 = Complex_Number(1,3), a new instance object is created in memory, and the name c1 binds with it.

On the command del c1, this binding is removed, and the name c1 is deleted from the corresponding namespace.

The object, however, continues to exist in memory. And if no other name is bound to it, it is later automatically destroyed.

This automatic destruction of unreferenced objects in Python is also called garbage collection.

Did you find this article helpful?