可以在Python中将类而不是对象作为方法参数吗?
我想做一些类似下面的事情
class A:
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
我希望这个能和 A.static_method()
一样。这样做可以吗?
3 个回答
0
当然可以!别忘了在静态方法前加上 @staticmethod。
class A:
@staticmethod
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
5
你应该能够做到以下几点(注意这个 @staticmethod
装饰器):
class A:
@staticmethod
def static_method_A():
print "hello"
def main(param=A):
param.static_method_A()
9
当然可以。在Python中,类是非常重要的对象。
不过,在你的例子中,你应该使用 @classmethod
装饰器(它的第一个参数是类对象)或者 @staticmethod
装饰器(没有初始参数)来定义你的方法。