DEV Community

Cover image for Python - Class & Object 🤔
Irena Popova 👩🏻‍💻
Irena Popova 👩🏻‍💻

Posted on

Python - Class & Object 🤔

A python is a pure object-oriented high-level programming language. What do I mean by that? Everything that is used is an object.

In this article, I will walk you through the object-oriented capability in python and how to create classes and objects with easy-to-understand examples.

What is a Class and the Object? 🤔

You can imagine a class is a blueprint for the encapsulation of information and the object is the instance of a class for real use. When an object is created, the actual memory is allocated for storing information.

The class implements functions/methods to access/modify the data in objects. So we can say that data is private and methods are public.

For example, in the real world, a person details is a class. That has a name, address, age, etc as information and methods setName(), getName, setAddress(), getAddress() etc as methods to access/modify information.

What are the Access specifiers in python?

The other object-oriented programming languages have access specifiers, private, public, and protected. Each specifier lets know the user/programmer about who can access the code.

Private (variable/functions) – Only the class members have access.
Public (variable/functions) – Members and can be accessed
anywhere from the object.
Protected – Can be accessed from class members or objects of the derived class (a class that inherits the class with protected members) only.

How to define a class and object in python?

A class in python can have data members and functions. The following example shows how we can create a class and can access its data members. The class declares two members and assigns integer values 1 and 2 respectively.

# Create a class 
class MyFirstClass:
    member_1 = 1;
    member_2 = 2;

classObject = MyFirstClass(); #creating object 
print(classObject.member_1); #printing first data member
print(classObject.member_2); #printing first data member
Enter fullscreen mode Exit fullscreen mode

How to add a member function in a class?

In the above print, we are printing members one by one, in the following example, we will show how we can add a function to the class for printing the data variables. Similarly, you can add any other function and implement the definition as you need.

# Create a class 
class MyFirstClass:
    member_1 = 1;
    member_2 = 2;

    def printMembers(self):
        print(self.member_1);
        print(self.member_2);


classObject = MyFirstClass(); #creating object
classObject.printMembers(); # Calling member function
Enter fullscreen mode Exit fullscreen mode

How to initialize data members at the time of object creation?

It is similar to the constructor in any other OOPs programming language. There is an init function. Whenever a program creates an object, python calls, or that class. It can take multiple types of parameters as arguments.

# Create a class 
class MyFirstClass:
    # initialization function for members.
    def __init__(self, input_1, input_2):
        self.member_1 = input_1;
        self.member_2 = input_2;


    def printMembers(self):
        print(self.member_1);
        print(self.member_2);


classObject = MyFirstClass(5,6); #creating object
classObject.printMembers(); # Calling member function

Enter fullscreen mode Exit fullscreen mode

Top comments (0)