【发布时间】:2009-05-12 02:35:23
【问题描述】:
我想做如下的事情
class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
我希望它等同于A.static_method()。这可能吗?
【问题讨论】:
标签: python static parameters
我想做如下的事情
class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
我希望它等同于A.static_method()。这可能吗?
【问题讨论】:
标签: python static parameters
当然。类是 Python 中的一等对象。
尽管在您的示例中,您应该为您的方法使用@classmethod(类对象作为初始参数)或@staticmethod(无初始参数)装饰器。
【讨论】:
您应该能够执行以下操作(注意 @staticmethod 装饰器):
class A:
@staticmethod
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
【讨论】:
当然,为什么不呢?不要忘记在静态方法中添加@staticmethod。
class A:
@staticmethod
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
【讨论】: