blog posts

The concept of class in Python and its relation to object-orientation

Classroom training in Python

Python programming language is one of the object-oriented languages ​​that supports all the features of object-oriented languages, so it is easy to create and use classes and objects in this programming language. 

Before addressing the topic of class definition in Python, you need to know about object-oriented programming language so that it is easy for you to understand.

Object-oriented programming (OOP), unlike procedural programming that focuses on sequential and explicit instructions, focuses on creating reusable patterns. That is, object-oriented programming when working on complex applications. The author allows you to reuse the code and write code that is more readable.


How to build a class in Python

The concept of class in Python

One of the most important concepts in object-oriented programming is the distinction between classes and objects, which is defined as follows:

In object-oriented programming, a class is a set of data (variables or attributes) and functions or methods that are members of a class.

Class data refers to variables that are input information stored in a class that attributes must be considered for each data in the class. In-class functions are called class methods that determine class behavior.

The concept of class in Python


Input data is first stored in class variables and then used by class methods. In fact, using the class definition, you can find the number of objects needed in the class.

The object is an example of a class that can be used, and the relationship between the object and the class is the same as the relationship between the house and the map, and as it is clear, using the drawn map, the house can be built and lived in. So any property that has a class will have an object or instance of it.


Compared to other object-oriented programming languages, the class structure in Python is a combination of the mechanisms found in C ++ and Modula. The class offers all the standard features of object-oriented programming, including:

  1. The inheritance mechanism is possible using several basic classes.
  2. An derived class can call each of its base classes by the same name.
  3. Available objects can contain arbitrary values ​​and data types.
  4. As with modules, classes can take advantage of the dynamic nature of Python, meaning that they are created at runtime and can be modified after creation.
  5.  Defined by the class keyword.
  6.  Each code is written in indentation.

Class features in Python

Class Definition in Python

As mentioned in the previous section, to define, you must first use the class keyword, then choose a name for your class, and finally write a two-dot operator. So the general rule is as follows:



As you can see in the above command, the code inside the class in Python is written with respect to the indentation relative to the first line of code.

Definition of behavior

To define behavior within a class in Python, you must create a function inside but with a name other than init, for example, a function called fullname that displays the firstname and lastname values ​​together.



In the above code, a function called fullname is created that has no input parameter.

In fact, here the self parameter is set by the interpreter and has an object on which the function is called.

Then in the code below, you can see that we have used the fullname function.



By executing the above code, Hossein Ahmadi is displayed in the output.

Existing objects

To define objects in a class in Python, you need to know that an object is an instance of a class. A class is like a plot, while an object is an instance of a plot with real values. An object contains the following:

state of:

The state of an object is displayed by the properties of that object and also reflects the properties of an object.


The behavior of an object is represented by the methods of that object and also represents the reaction of an object with other objects.


A unique name is given to an object and enables an object to communicate with other objects.

Existing objects 

How to build an object

To access members of a class, they must be accessed through an object. After creating a class, you must first create an instance of the class in Python. As you are aware, an instance of a class is called an object, which is actually an instance variable of the corresponding class.

How to build an object 

The first thing to do after creating a class is to create a instance of the class. As you are aware, an instance of a class is called an object, which is actually an instance variable of the corresponding class.

In the Python programming language, all variables that are created from standard Python variants are instances of a class in Python. The following code snippet creates class variables that make up the most common data types in Python.



To use it, you must first create an object of that class before doing anything. The following code shows how to create a class object in Python.



The following code is an example of defining a class in Python:



 The concept of empCount

Is a class variable that is common to all class instances and can be accessed from inside and outside the class. The __init__ method is a special method called the class constructor or the initial value of the class giver, and whenever a new object of a class is created in Python, Python calls these methods.

Other methods can be defined as functions, except that the first parameter must be self, but when calling methods, Python itself creates the self parameter and does not need to be set by the programmer.

 The concept of empCount

As you know, to create an object from a class in Python, it can be called using the class name, and its argon can be set to values ​​defined by the __init__ method.



Use the phrase pass

The pass statement in the class definition in Python is actually the Null operator, which does not happen when the program code is executed. In fact, when we want to define a class by its name and then its commands and body, we have to use the phrase pass. Consider the following example:



sample1 and sample2 are both examples of the SampleClass1 and SampleClass2 classes, respectively, but since the body body of the class is not yet defined, both objects have no content.

Difference between sample variables and class variables in Python

Sample variables for data are unique to each instance, but class variables are for the attributes and methods that are shared by all class instances in Python.

In fact, sample variables are variables whose value is assigned inside a constructor or method, while class variables are variables whose value is assigned to a class. Note the following code:



As discussed in one word about names and objects, shared data can have amazing effects by inserting variable objects such as lists and dictionaries. For example, the list of tricks in the following code should not be used as a class variable in Python because only a single list is shared by all Dog items:



Call class in Python

Each class in Python can have different methods, which are the same functions as in the class. Methods can add other properties to the class or do a specific task. Consider the following written class.



Call the whole module

Calling a class is like calling a module. There are two ways to call a class:
1. Calling the whole class module in Python with the Import command: Note the following code:



2. Call one or more specific classes of the class module using the two commands import and from



It should be noted that as long as the class module name is called using the import command, the written description of the class and its methods can be extracted using the help command. You can also use the help command to show the description of the functions written in the document.



We hope that this article has been of interest to you dear programmers and that you can use it well in projects when using the class in Python.