python 在不初始化 class 的情况下调用 class 方法
python calling a class method without initializing the class
我想从 python class 调用一个函数,但是有必要对其进行初始化,所以有没有一种方法可以在不初始化 [=17] 的情况下调用 class 方法=].我有js背景我想要的和这个差不多
class foo {
static calculate() {
return 'bar';
}
}
您可以使用@staticmethod
装饰器在python中定义静态方法:
class Class:
@staticmethod
def method():
print("Method called")
Class.method()
我想从 python class 调用一个函数,但是有必要对其进行初始化,所以有没有一种方法可以在不初始化 [=17] 的情况下调用 class 方法=].我有js背景我想要的和这个差不多
class foo {
static calculate() {
return 'bar';
}
}
您可以使用@staticmethod
装饰器在python中定义静态方法:
class Class:
@staticmethod
def method():
print("Method called")
Class.method()