Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-classmethod()_Python_Generator_Standard Library_Class Method - Fatal编程技术网

Python-classmethod()

Python-classmethod(),python,generator,standard-library,class-method,Python,Generator,Standard Library,Class Method,阅读Python标准库。试图理解classmethod class C: @classmethod def f(x,y): print('words') 当我打字时: print(classmethod(C)) 它返回: <classmethod object at 0x00FAD930> 如何查看classmethod返回的内容而不是classmethod的生成器?更改为: class C: @classmethod def f

阅读Python标准库。试图理解classmethod

class C:
    @classmethod
    def f(x,y):
        print('words')
当我打字时:

print(classmethod(C))
它返回:

<classmethod object at 0x00FAD930>
如何查看classmethod返回的内容而不是classmethod的生成器?

更改为:

class C:
    @classmethod
    def f(cls, x, y):
        return 'words'
与第一个参数是自引用的传统类方法不同,类方法作为隐式第一个参数接收类,就像实例方法接收实例一样

另外,您需要返回一个值来打印它。

您正在生成一个classmethod描述符对象:

print(classmethod(C))
返回包装在新classmethod对象中的类C。这不是生成器,这是Python在用它装饰方法时实际存储在类中的内容

记住,函数或类上方的@decorator_表达式行只是替换该对象的额外函数调用的语法糖@上面定义的类方法。。。这仅仅意味着Python将用f=classmethodf替换f

当查看C.uuu dict_uuu['f']值时,可以看到相同类型的对象;这是相同类型的对象:

>>> class C:
...     @classmethod
...     def f(x,y):
...         print('words')
... 
>>> C.__dict__['f']
<classmethod object at 0x107d00810>
>>> C.__dict__['f'].__get__(None, C)
<bound method type.f of <class '__main__.C'>>
>>> C.f
<bound method type.f of <class '__main__.C'>>
方法不是通常的自实例对象,而是向类传递一个引用。对于直接对类的C.f调用和对C实例的C.f调用,f的第一个参数是C本身

将其与C.常规方法进行比较;这是一个普通的方法,当直接在带有C.regular的类上调用时,没有传入任何参数;当在带有C.regular的实例上调用时,传入了第一个参数实例。这就是通常在方法签名中使用self的原因

对于类方法,在声明方法时,第一个参数通常命名为cls。

如果要调用在C上创建的类方法f,应执行以下操作:

C.f(...)
是decorator函数,它将包装传递给它的任何内容并返回classmethod对象:

这使得:

>>> C.f(1, 2)
words
或者,返回值并在函数外部打印:

class C:
    @classmethod
    def f(cls, x, y):
        return "words"

>>> print(C.f(1, 2))
words

在创建的所有不同对象之间共享值时,classmethod非常有用

class Classtest:
    a =10
    @classmethod
    def hi(cls, x):
        cls.a= cls.a+x
        print cls.a

h = Classtest()
h.hi(10)

b = Classtest()
b.hi(30)

c = Classtest()
c.hi(40)

>>> ================================ RESTART     ================================
>>>  
20
50
90

在这里,变量a的值在正在创建的所有不同对象之间共享,而不仅仅限于一个实例

您没有classmethod的生成器。您有classmethod描述符对象。是否尝试过:C.f1,2?
>>> C.f(1, 2)
words
class C:
    @classmethod
    def f(cls, x, y):
        return "words"

>>> print(C.f(1, 2))
words
class Classtest:
    a =10
    @classmethod
    def hi(cls, x):
        cls.a= cls.a+x
        print cls.a

h = Classtest()
h.hi(10)

b = Classtest()
b.hi(30)

c = Classtest()
c.hi(40)

>>> ================================ RESTART     ================================
>>>  
20
50
90