Building class and creating object in Python

This post is lesson 32 of 54 in the subject Python Programming Language

1. Python is an object-oriented programming language

Python is an object-oriented programming language (OOP). It is common to build classes and create objects in Python. In Python, almost everything is an object with its properties and methods.

For example, when a data value is assigned to a variable in Python, the variable refers to an object that stores the data value. This means that every data value in Python is considered an object. For more information on how Python handles variable creation, see this resource.

To learn about OOP in Python, you may find it helpful to refer to the series of lessons on Object-Oriented Programming Methodology. These lessons will help you comprehend OOP using C++, a fundamental concept that will help you learn and grasp OOP in other languages much more quickly.

2. Building class in Python

A class is a blueprint or prototype for objects of the same type. For example, we have a Cat class that contains all the information about a cat such as name, color, weight, catching mouse action, etc. A specific cat is an object and will have the common information of a cat but with different values.

Example of cat object
Example of cat objects

In Python, we use the class keyword to create a class.

class Cat:
  # Properties of Cat class
  name = "Tom"
  color = "grey and white"
  # Methods of Cat class
  def catchMouse(self):
    print("Catch Jerry mouse.")
  def sleep(self):
    print("Take a nap.")

In the above example, we define a class Cat including the properties name, color, and methods catchMouse() and sleep().

2.1. The self parameter of the class in Python

The self parameter is a reference to the current object of the class and is used to access the variables belonging to that class.

class Cat:
  # Properties of Cat class
  name = "Tom"
  color = "grey and white"
  # Methods of Cat class
  def catchMouse(self):
    print(self.name, "catches Jerry mouse.")
  def sleep(self):
    print(self.name, "takes a nap.")

This parameter can have any name you choose, but Python will use the first parameter of any function in the class as the default self parameter.

class Cat:
  # Properties of Cat class
  name = "Tom"
  color = "grey and white"
  # Methods of Cat class
  def catchMouse(abc):
    print(abc.name, "catches Jerry mouse.")
  def sleep(abc):
    print(abc.name, "takes a nap.")

2.2. The pass statement with class in Python

The definition of a class cannot be empty. But if we do not know the properties and methods of the class to define, we can use the pass statement in the class to avoid errors.

class Person:
    pass

3. Creating an object in Python

An object is a specific instance of a class. We can use the name of the class to create an object. When an object is created, Python will allocate memory to store that object.

class Cat:
  # Properties of Cat class
  name = "Tom"
  color = "grey and white"
  # Methods of Cat class
  def catchMouse(self):
    print(self.name, "catches Jerry mouse.")
  def sleep(self):
    print(self.name, "takes a nap.")

# create an object of Cat class
tom = Cat()
print(tom.name)
print(tom.color)
tom.catchMouse()
tom.sleep()

Output

Tom
grey and white
Tom catches Jerry mouse.
Tom takes a nap.

We can delete an object with the keyword del.

class Cat:
  # Properties of Cat class
  name = "Tom"
  color = "grey and white"
  # Methods of Cat class
  def catchMouse(self):
    print(self.name, "catches Jerry mouse.")
  def sleep(self):
    print(self.name, "takes a nap.")

tom = Cat()
print("Before delete tom object:", tom)
del tom
print("After delete tom object:", tom)

Output

Before delete tom object: <__main__.Cat object at 0x0000023672FE7D60>
Traceback (most recent call last):
  File "c:\python-examples\example.py", line 14, in <module>
    print("After delete tom object:", tom)
NameError: name 'tom' is not defined

When building a class and creating an object, a class always has a built-in function __init__() which is the class’s constructor. We often need to rebuild the __init__() function when creating a class. In the lesson The __init__() constructor function of a class in Python, we will cover additional information on the __init__() function.

Rate this post
Previous and next lesson in subject<< Handling exceptions in PythonThe __init__() constructor function of a class in Python >>

Leave a Reply

Your email address will not be published. Required fields are marked *