小编key*_*ian的帖子

为什么从实例获取类属性会引发 AttributeError?

通常,您可以从该类的实例访问常规类属性/字段。但是,当尝试访问类属性时,会引发 AttributeError。为什么实例看不到类对象的属性?

class Meta(type):

    @property
    def cls_prop(cls):
        return True


class A(metaclass=Meta):
    cls_attr = True


A.cls_attr # True
A.cls_prop # True
a = A()
a.cls_attr # True
a.cls_prop # AttributeError: 'A' object has no attribute 'cls_prop'
Run Code Online (Sandbox Code Playgroud)

python properties metaclass class attributeerror

2
推荐指数
1
解决办法
1205
查看次数

标签 统计

attributeerror ×1

class ×1

metaclass ×1

properties ×1

python ×1