首页 > 解决方案 > 为什么 Python 实例变量不起作用?

问题描述

为什么实例变量b不起作用:

class Clothing:
    a = 5

    def __init__(self):
        self.b = 0

shirt = Clothing
shirt.b

AttributeError:类型对象“服装”没有属性“b”

为什么尝试访问shirt.b 会引发属性错误?

标签: pythonpython-3.xoopinstance

解决方案


你需要括号,它应该是shirt = Clothing()


推荐阅读