Can python have class or instance methods that do not have “self” as the first argument?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Why do you need explicitly have the"self" argument into a Python method?
Python ‘self’ explained
This is just for edification我自己。学习Python。Python面向对象与moved and have"。example of a method在每个单在班,我见过有"自我"as the first argument。这是真正的法学院?if it is true,have been T不知道Python所写的that this argument was just not needed understood和therefore?谢谢。P></
如果需要不需要访问
1 2 3 4 5 6 7 8 9 10 | class C(object): def my_regular_method(self, foo, bar): pass @staticmethod def my_static_method(foo, bar): pass c = C() c.my_regular_method(1, 2) c.my_static_method(1, 2) |
如果要访问类,但不访问实例,请使用
1 2 3 4 5 6 | class C(object): @classmethod def my_class_method(cls, foo, bar): pass c.my_class_method(1, 2) |
静态方法不需要self,它们在类上操作
请参阅下面关于静态的一个很好的解释:python中的静态类变量