Python classmethod() Function

“Classes and functions are the building blocks of an object-oriented programming language. You cannot write good-quality code without using functions and classes. Also, they help you write structured codes which are properly organized so that anyone can understand them by looking at them. In this article, the classmethod() function of python is discussed. We will help you learn to use the classmethod() function with the help of useful examples. This article is a short guide for all beginner python developers. Refer to all the information provided here if you are new to this concept.”

What is a Python classmethod() Function?

Let us first give a quick introduction to the python class method. It is defined as a method that is accountable to the class rather than its object. As the parameter is the class itself, we can say that the class method works with the class.

The python classmethod() is a built-in function of the python programming language. It is used to create a class method of a specified function. The function name is given to the function as a parameter, and the classmethod() converts the specified function into a class method. Whenever we try to write a proper code following all the object-oriented norms, classmethod() comes in handy. It helps you create a class method quickly by only using a single built-in function, classmethod()

Syntax of Python classmethod() Function

The syntax of the python classmethod() function is straightforward and easy to learn. All you have to do is call the classmethod() function by passing the function name. See the syntax of the function below:

The python classmethod() function takes the “function_name” as a parameter that should be passed to the function. The classmethod() function is a bit outdated in python programming language and considered un-pythonic. The @classmethod decorator is used in the new versions of python for class method definition. The syntax of @classmethod is as follows:

The @classmethod is the classmethod() function that converts the function into a class method. The “function” is the created function that classmethod() will convert into a class method. Now let us write some code in the python programming language to understand the classmethod() function. The examples coming in the following section are very easy, you can replicate these codes, or if you want, you can modify them according to your desired practical application.

Example 1

This case is a basic and easy example of the classmethod() function. In the first line of the code, we are defining the class as human and defining the variable gender as “male”. Next, we will use a user-defined function called “display” and print the statement “The gender of human is:”, cls.Gender with print() function. Now we will use the function of classmethod() and run the code to show what the gender of the human is by using Human.display(). Now we will run the following code:

class Human:

  Gender = "Male"

  def display(cls):

    print('The gender of human is:', cls.Gender)

Human.display = classmethod(Human.display)

Human.display()

As we can see from the output, the results show that the gender of the human is male.

Example 2

In our next case, we will be looking for a situation in which we want to purchase or buy a book on a specific subject course. This example showcases the classmethod() function by using the class named “subject”. First of all, we define the class as a “subject”. Let us define the subject as an algorithm. The subject variable is defined as “course,” and “algorithm” is the value assigned to it. This example will have the user function of “buy,” and we print the statement to purchase a book of the class subject. Here we will use the classmethod() function and run the code to know which subject to buy.

class subject:

  course = 'Algorithm'

  def buy(cls):

    print("Book purchased is = ", cls.course)

subject.buy = classmethod(subject.buy)

subject.buy()

The result we get from running the code is as follows:

We can see from the output after running the code that the book that we need to purchase is of “algorithm.”

Example 3

To assist you in comprehending how to use the class method decorator in your Python scripts, we’ll use it in this example. We will import the date from the datetime library in the first line of code. The second code line defines the class as human, after which a user function named in it is defined. Under that function, a variable is declared named “self.n”, and the “n” value is assigned to it. The other variable is declared as “self. A”, with the value “a” assigned to it. Now using the @classmethod function, we define the user function as “Birthday” and then input the date and year in the function to pass through @classmethod.

Before using the class method function, we will define one other user define a function which is “display”. In this code, we are commanded to print the name of the person who is declared as “self.n” as well as the age of the person who is declared as “self.a”. Using the last input command function of human.display(), we run the code and get the following output.

The output here shows that the name of the person is Kalsoom and the age of the person is 25. As we can see in the above example, when a function name is given to the function as a parameter, the class method will convert that specific function into a class method.

The function name is given to the function as a parameter, and the classmethod() converts the specified function into a class method

Conclusion

This article was a quick guide about the classmethod() function in the python programming language. All you have to do is to pass the name of the function to the classmethod() function, and it will create the class method of the specified function. The classmethod() function is now considered un-pythonic, so the @classmethod decorator can be used instead. We explored some useful examples to help you understand how to use both classmethod() function and the @classmethod decorator.



from https://ift.tt/G0l1c5f

Post a Comment

0 Comments