Python 简明教程
Python - Static Methods
What is Python Static Method?
在 Python 中, static method 是一种无需调用任何实例的方法。它与类方法非常相似,但不同之处在于静态方法没有 − self 对象的引用或对类 − cls 的引用的强制参数。
In Python, a static method is a type of method that does not require any instance to be called. It is very similar to the class method but the difference is that the static method doesn’t have a mandatory argument like reference to the object − self or reference to the class − cls.
静态方法用于访问给定类中的静态字段。因为它们与类绑定,而不是与实例绑定,所以它们不能修改类的状态。
Static methods are used to access static fields of a given class. They cannot modify the state of a class since they are bound to the class, not instance.
How to Create Static Method in Python?
有两种方法可以创建 Python 静态方法 −
There are two ways to create Python static methods −
-
Using staticmethod() Function
-
Using @staticmethod Decorator
Using staticmethod() Function
Python 的名为 staticmethod() 的标准库函数用于创建静态方法。它接受一个方法作为参数,并将其转换成一个静态方法。
Python’s standard library function named staticmethod() is used to create a static method. It accepts a method as an argument and converts it into a static method.
Example
在下方的 Employee 类中,showcount() 方法转换成一个静态方法。现在可以通过其对象或类本身的引用来调用这个静态方法。
In the Employee class below, the showcount() method is converted into a static method. This static method can now be called by its object or reference of class itself.
class Employee:
empCount = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
Employee.empCount += 1
# creating staticmethod
def showcount():
print (Employee.empCount)
return
counter = staticmethod(showcount)
e1 = Employee("Bhavana", 24)
e2 = Employee("Rajesh", 26)
e3 = Employee("John", 27)
e1.counter()
Employee.counter()
执行以上代码将打印以下结果 −
Executing the above code will print the following result −
3
3
Using @staticmethod Decorator
创建静态方法的第二种方法是使用 Python @staticmethod 装饰器。当我们对一个方法使用这个装饰器时,它向解释器指示指定方法是静态的。
The second way to create a static method is by using the Python @staticmethod decorator. When we use this decorator with a method it indicates to the Interpreter that the specified method is static.
Example
以下示例中,我们使用 @staticmethod 装饰器创建一个静态方法。
In the following example, we are creating a static method using the @staticmethod decorator.
class Student:
stdCount = 0
def __init__(self, name, age):
self.__name = name
self.__age = age
Student.stdCount += 1
# creating staticmethod
@staticmethod
def showcount():
print (Student.stdCount)
e1 = Student("Bhavana", 24)
e2 = Student("Rajesh", 26)
e3 = Student("John", 27)
print("Number of Students:")
Student.showcount()
运行以上代码将打印以下结果 −
Running the above code will print the following result −
Number of Students:
3
Advantages of Static Method
使用静态方法有许多优势,包括 -
There are several advantages of using static method, which includes −
-
Since a static method cannot access class attributes, it can be used as a utility function to perform frequently re-used tasks.
-
We can invoke this method using the class name. Hence, it eliminates the dependency on the instances.
-
A static method is always predictable as its behavior remain unchanged regardless of the class state.
-
We can declare a method as a static method to prevent overriding.