Class Method in Python
The class method is linked to the class itself and not to any class objects. Also, only class variables are accessible to it. Altering a value of such a class variable affects all class objects. We declare the method as a class method whenever we utilize class variables while implementing a technique. The class is referenced by a keyword ‘cls’ which is the first parameter of a class method. When working with factory techniques, we utilize class methods. Factory methods comprise those that, depending on the use case, return a class object.
By using the @classmethod decorator as well as classmethod() function, we must explicitly notify Python that the method is indeed a class method. When class methods were defined, the process is quite similar to that of establishing a standard function. Similarly, to obtain class variables from within class methods, we utilize a ‘cls’ keyword as the primary argument. As a result, we have control over updating the class state due to the class method. While it is possible to use a variable with a different name for ‘cls’, doing so is discouraged given that self is the preferred norm in Python. The class method cannot access instance attributes; it will only access class attributes.
Static Method in Python
An independent task can be completed using a static method, which is a generic utility method. Python’s static methods are not exactly comparable to those of Java but rather closer to C++. Even though a static method does not contain an implicit initial parameter, like self as well as ‘cls’, it cannot access the class but also instance variables.
Every method we add to a class will be converted directly into an instance method. A @staticmethod decorator and otherwise staticmethod() function must be used to expressly indicate to Python that the method is indeed a static one. It is quite similar to constructing a regular function to establish static methods inside a class.
The class method utilized access as well as altered the state of the class. By altering a value of such a class variable which affects all class objects, it can change the state of either class.
Due to their lack of access to object properties (instance variables) as well as class attributes, static methods are only sometimes used (class variables). Therefore, they can indeed be useful in some situations, such type conversion.
As a factory method, class methods are utilized. Factory methods include those that, depending on the use case, return the class object. For instance, before generating an object, you must perform certain pre-processing upon that data supplied.
Example no 1
Let’s see how to utilize the static method in the code.
Initially, we construct a class called “class Emp 1” for employee 1. We will be utilizing a static method, specifying a sample ‘y’, as well as printing the results by the use of the print command. After that, we now called “Emp_1.sample(),” a static function that may also be invoked using the object “emp1 = Emp_1().”
Example no 2
When specifying the class method, use @classmethod decorator or perhaps the classmethod() function. To create a static method, just use staticmethod() function or @staticmethod decorator.
Whenever defining a class method, utilize ‘cls’ as the very first parameter. Referring to the class is the ‘cls’. Due to the lack of access to instance variables as well as class variables, static methods cannot accept attribute and class as a parameter.
We start by declaring a class called ‘Student’ as well as giving it the variable ‘school_name’. Next, we would create a constructor. The instance variables self.name and self.id are declared. Following that, we implement the class method by creating the function “change_School()”. This method includes the “cls” and “name” parameters. The static method is therefore used. We define the method “find_notebook()” and then pass the attribute “notebook_name”. The return command is then utilized to produce the desired result.
Example no 3
Attributes exist for both objects and classes. Class variables are contained within class attributes, while instance variables are part of object attributes. Just class-level characteristics are accessible through class methods. This can indeed alter the class state.
In this illustration, a class called “Worker” with a variable named “location_name” was created. We created a function called init() that has the variables self, name, as well as id. Furthermore, we access two different class variables and instance variables, correspondingly, by using the show() method and the print() function twice. The first print command consists of ‘worker’, ‘self.name’, and ‘self.id’ as the parameters. Similarly, the second print command includes ‘location’, ‘self.location_name’ as the arguments. Then, by solely accessing class variables, we called a class method as well as defined the function change_Location(). The print() function would be applied two more times by the class method and also its parameter “cls”. The static method will be constructed. Within this methodology, the find_toolbox() function is defined. The ‘return’ command is employed. Performing these actions, we created the object “anis” and invoked the appropriate show() method.
Example no 4
Static as well as class methods are tied to that same class. Hence, the class name ought to be utilized to access them.
Within this example, we create the class “Player” as well as define the init() method, which has two variables self and team_no in it. The class method and static method would be constructed. Within the class method, we define the change_city() function which has two arguments. The function find_accessories() will be defined inside the static method. This function has only one argument. Subsequently, under the instructions, we called the print() function twice to show the static as well as class methods. After creating an object called “hammad” we individually bonded the class as well as static methods to it.
In this guide, we spoke about class as well as static methods in Python, including what they are, how they’re defined, and how to create them. To help you understand what is happening inside the examples, as well as how one should utilize these functions throughout their coding journey, we also included some details that distinguished between these two methods. In each of these instances, class methods, as well as static methods in Python, are used, along with instance methods where appropriate.