{"id":26165,"date":"2021-12-14T08:46:26","date_gmt":"2021-12-14T03:16:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=26165"},"modified":"2021-12-14T08:46:26","modified_gmt":"2021-12-14T03:16:26","slug":"python-classmethod-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-classmethod-with-examples\/","title":{"rendered":"Python classmethod() with Examples"},"content":{"rendered":"
Python classmethod() is a built-in Python standard library function.<\/p>\n
Python methods are classified into three types:<\/p>\n
Static Method vs. Class Method<\/strong><\/p>\n Let us explore and concentrate on Python’s class method.<\/p>\n classmethod() in Python:<\/strong><\/p>\n Python’s classmethod() function returns the class method for a given function. It is a built-in method in python.<\/p>\n A Python classmethod() can be invoked by both a class and an object.\u00a0It is a method that all things have in common. It is a built-in feature of the Python programming language.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong> This function takes the function name as an argument.<\/p>\n Return Value:<\/strong><\/p>\n The converted class method is returned by the classmethod().<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The @classmethod decorator is a built-in function decorator that is an expression that is evaluated after you create your function. The outcome of that evaluation shadows\u00a0your function definition.<\/p>\n The class is sent as the implicit first argument to a class method, just as the instance is passed to an instance method.<\/p>\n We can invoke the class name instead of the object by utilizing the decorator method. It can be applied to any of the class’s methods.<\/p>\n Syntax:<\/strong><\/p>\n function:<\/strong> It is the function to be converted into a class method. For Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The @classmethod decorator has applied to the Getsalary() function in the above example. It has a single parameter called cls_obj<\/strong>. The method then selects a member variable of the class automatically rather than requiring the user to pass one to the function.<\/p>\n","protected":false},"excerpt":{"rendered":" Python classmethod() is a built-in Python standard library function. Python methods are classified into three types: Static Method Instance Method Class Method Static Method vs. Class Method A class method requires class as its initial parameter, but a static method does not. A static method cannot access or modify the class state, but a class …<\/p>\n\n
classmethod(function)<\/pre>\n
\n
# Create a class say Employee\r\nclass Employee:\r\n # Inside the class, take a variable and initialize it with some random number.\r\n salary = 30000\r\n # Create a function\/method say Getsalary() which accepts the class\r\n # object as an argument\r\n\r\n def Getsalary(cls_obj):\r\n # Print the class variables by using the class object.\r\n print(\"The salary of an Employee = \", cls_obj.salary)\r\n\r\n\r\n# Pass the Getsalary() method to the classmethod() function and store it as the\r\n# same function(Employee.Getsalary).\r\nEmployee.Getsalary = classmethod(Employee.Getsalary)\r\n# Call the above function.\r\nEmployee.Getsalary()\r\n<\/pre>\n
The salary of an Employee = 30000<\/pre>\n
@classmethod Decorator:<\/strong><\/h5>\n
class c(object):\r\n @classmethod\r\n def function(cls, argument1, argument2, ...):\r\n \/*rest of the code*\/<\/pre>\n
\nReturn Value:<\/strong> A class method for the function is returned.<\/p>\n\n
\n
# Create a class say Employee\r\nclass Employee:\r\n # Inside the class, take a variable and initialize it with some random number.\r\n salary = 30000\r\n\r\n # Apply the @classmethod annotation for the function to access the function\r\n # outside the class instead of creating an object.\r\n @classmethod\r\n # Create a function\/method say Getsalary() which accepts the class\r\n # object as an argument\r\n def Getsalary(cls_obj):\r\n # Print the class variables by using the class object.\r\n print(\"The salary of an Employee = \", cls_obj.salary)\r\n\r\n\r\n# Call the above function with the class name.\r\nEmployee.Getsalary()\r\n<\/pre>\n
The salary of an Employee = 30000<\/pre>\n