我想做类似下面的事情
class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
我希望它等同于 A.static_method()
。这可能吗?
最佳答案
当然。类是 Python 中的一流对象。
尽管在您的示例中,您应该为您的方法使用 @classmethod
(类对象作为初始参数)或 @staticmethod
(无初始参数)装饰器。
关于python - 您可以将类(不是对象)作为参数传递给 python 中的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/850921/