[英]What is the difference between __main__ and class '__main__?
I am learning about classmethod. 我正在学习类方法。 I have looked at this example
我看了这个例子
class A(object):
def foo(self,x):
print (self,x)
@classmethod
def class_foo(cls,x):
print(cls,x)
@staticmethod
def static_foo(x):
print (x)
a=A()
a.foo('pic')
a.class_foo('pic')
This is output 这是输出
<__main__.A object at 0x7f413121c080> pic
<class '__main__.A'> pic
What is the practical meaning of this?Implementation? 实施的实际含义是什么?
Implementation-wise, a classmethod
takes the first obligatory cls
argument, which is not the case for a staticmethod
. 在实现方面,
classmethod
采用第一个强制性cls
参数,而staticmethod
则不是这种情况。
The practical meaning is that you can call a classmethod
without having to create a class object first. 实际含义是,你可以调用一个
classmethod
,而无需首先创建一个类的对象。 In code this is perfectly valid: 在代码中,这是完全有效的:
A.class_foo('pic')
You can read more about this subject on this excellent SO post. 您可以在这篇出色的SO帖子上阅读有关此主题的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.