我在访问对象属性时遇到问题。任务本身正在创建一系列算法来比较多个对象的属性,但是考虑到我还不能访问这些属性,我甚至走不了那么远。在
我写了一段代码,与下面我正在处理的代码类似。当我试图访问list_of_things.items[0].attribute1
时,我遇到了问题。我试图打印只是为了确保我正确地访问项目,但我得到了以下错误:
Traceback (most recent call last):
File "./test.py", line 22, in <module>
print(list_of_things.items[0].attribute1)
AttributeError: 'function' object has no attribute 'attribute1'
类似代码如下:
^{pr2}$我不能改变任何一个班级,但我将增加我的作业定义。在
问题:
所以,根本问题就是错误信息的含义:
这是因为
^{pr2}$items[0].attribute1
试图访问函数对象上的attribute
,因为items[0]
是一个函数对象。注:认识到
^{3}$one_thing.give_a_thing
返回方法本身,您希望调用该方法:除此之外,这段代码的结构非常奇怪。为什么
give_a_thing
只是返回对象本身?这意味着您的list_of_things
将是一个包含对同一对象的多个引用的列表。在你可能想要
然后简单地:
相关问题 更多 >
编程相关推荐