3.2.1 静态函数与类方法
class Person:
@staticmethod # 用 staticmethod 这个修饰器来表明函数是静态的
def sayHello():
print('Hello')
Person.sayHello() # 输出 'Hello`class Person:
@classmethod # 用 classmethod 这个修饰器来表明这是一个类方法
def sayHi(cls):
print('Hi: ' + cls.__name__)
Person.sayHi() # 输出 'Hi: Person`# 在 module1.py 文件中:
def global():
pass
class Util:
@staticmethod
def helper():
pass
# 在 module2.py 文件中:
import module1
module1.global() # 调用全局函数
module1.Util.helper() # 调用静态函数Last updated