python - 为什么 Python 实例变量不起作用?
问题描述
为什么实例变量b不起作用:
class Clothing:
a = 5
def __init__(self):
self.b = 0
shirt = Clothing
shirt.b
AttributeError:类型对象“服装”没有属性“b”
为什么尝试访问shirt.b 会引发属性错误?
解决方案
你需要括号,它应该是shirt = Clothing()
推荐阅读
- unit-testing - 在 Visual Studio Code 编辑器中完全禁用 Jest 测试运行器的自动运行
- html - 背景颜色不适用
- python - 将 Excel 文件转换为 CSV 并进行一些更改
- python - 使用 xpath 提取值会产生一些不需要的字符串
- reactjs - 反应路由器 dom 重定向问题。更改 url,不渲染组件
- python - 从 pandas 数据帧构造一个二维数组
- python - pyinstaller 因权限错误而失败 [Err-13]
- javascript - Bootstrap Datepicker 在使用 javascript 进行动态元素追加时不起作用
- java - 通过 Python 执行外部软件
- python - 以未指定格式处理 url 流的音频