- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
据说:
When it would yield a class method object, it is transformed into a bound user-defined method object whose im_class and im_self attributes are both C.
我做了一个 EX。
>>> class C(object) :
... @classmethod
... def cm(cls) : print cls
...
>>> C.cm
<bound method type.cm of <class '__main__.C'>>
>>> C.cm.im_self
<class '__main__.C'>
>>> C.cm.im_class
<type 'type'>
我不难理解这种现象。但不幸的是,在引用资料中,它被告知 im_self 应该与 im_class 相同。如何解释不一致?
最佳答案
我读的和你一样。看来 Python 实际执行的操作与文档中所说的并不完全相同。
它将 im_self
设置为类,将 im_class
设置为类的类型,即它的元类。 Python 中类的默认元类是 type
。这类似于绑定(bind)到实例的方法所发生的情况:im_self
是实例,im_class
是实例的类型。在 @classmethod
的情况下,换句话说,类被视为实例(它是;它是 type
的实例)。
可能在没有更新文档的情况下更改了行为,或者文档一开始就是错误的。我以编写文档为生,我可以确认,对于 Python 大小的东西,要使其 100% 正确几乎是不可能的——尤其是对于像这样晦涩的细节。
Python 开发人员在文档中有报告错误的程序。 Give it a try !
关于Python 数据模型 : confused again with classmethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9107205/
根据Python docs : If a class method is called for a derived class, the derived class object is passed
假设我有一个类 class SimpleGenerator(object): @classmethod def get_description(cls): return
我必须使用一些 Python 库,其中包含具有各种实用程序的文件:类和方法。其中一种方法以下列方式定义(我不能放完整代码): @classmethod def do_something(cls, ar
在 Python 中,我可以使用 @classmethod 创建一个类方法。装饰师: >>> class C: ... @classmethod ... def f(cls): ...
我通常使用 isinstance 进行构造函数重载,但人们也建议使用 @classmethod 来实现相同的目的。但据我所知,@classmethod 共享该变量。 下面是一个简单的类 class a
遇到这个难题时,我正在学习 Python 中的类和对象。下面是相同代码的两种情况,一种没有@classmethod,另一种有@classmethod: #without @classmethod >>
装饰器classmethod的源代码在python源码在哪里。具体来说,我无法找到它在 2.7.2 版中定义的确切文件 最佳答案 我没有回答你的问题 - 但下面的代码显示了用纯 Python 编写的等
TL;DR 如何确定一个函数是使用 @classmethod 还是具有相同效果的东西定义的? 我的问题 为了实现一个类装饰器,我想检查一个方法是否将类作为它的第一个参数,例如通过 @classmeth
我有以下 Python 元类,它为每个类添加了一个 deco_with_args 装饰器: def deco_with_args(baz): def decorator(func):
我想使用@classmethod,但我不想用它污染实例的命名空间。如果我有一个类方法 for_classes_only,如何防止实例获取它? class MyThing(object): de
这个问题已经有答案了: What is the purpose of class methods? (18 个回答) 已关闭 4 年前。 我观看了一个 YouTube 视频,解释了 @classmet
这更像是一个过程问题,但我现在用 Python 编程已经有一段时间了,我试图理解单元测试、功能测试之间的区别,以及何时适本地使用模拟以便测试功能。我有以下安排: @classmethod def ge
据说: When it would yield a class method object, it is transformed into a bound user-defined method ob
我尝试在带有装饰器@classmethod 的方法中调用方法,知道如何实现吗?例如我有: class A(): def B(self): #do something retur
我有以下代码来查找数据库中的对象实例,然后使用数据创建 python 对象。 class Parent: @staticmethod def get(table, **kwargs):
我有这个谷歌应用程序引擎数据存储区 NDB 模型: class pySessions(ndb.Model): # sid is the key/id data = ndb.Blobpr
我有一个装饰器叫做 Special将函数转换为自身的两个版本:一个可以直接调用并在结果前加上前缀 'regular '和一个可以用 .special 调用的并在结果前加上 'special ' 前缀:
我想猴子修补一个类方法,保留旧功能。考虑我的代码来理解这个想法。这是我的代码(非常综合的例子)。 #!/usr/bin/env python class A: @classmethod def
我在 python 中基本上有以下内容: class X(object): pass class Y(object): @classmethod def method(cls, x_inst,
我有以下代码: class ObjectOne(object): @classmethod def print_class_name(cls): print cls._
我是一名优秀的程序员,十分优秀!