访问对象';在python中,对象数组属性给出属性错误
我在访问对象属性时遇到问题。分配本身创建了许多算法,比较了许多对象的属性,但考虑到我无法访问这些属性,我甚至不能做到这一点 我已经编写了一段代码,与下面的工作类似。我遇到问题的地方是当我尝试访问访问对象';在python中,对象数组属性给出属性错误,python,python-3.x,oop,object,Python,Python 3.x,Oop,Object,我在访问对象属性时遇到问题。分配本身创建了许多算法,比较了许多对象的属性,但考虑到我无法访问这些属性,我甚至不能做到这一点 我已经编写了一段代码,与下面的工作类似。我遇到问题的地方是当我尝试访问事物列表时。项[0]。属性1。我尝试打印只是为了确保正确访问该项目,但出现以下错误: Traceback (most recent call last): File "./test.py", line 22, in <module> print(list_of_things.ite
事物列表时。项[0]。属性1
。我尝试打印只是为了确保正确访问该项目,但出现以下错误:
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'
我无法更改任何一个类,但将为我的作业添加def
问题:
因此,根本问题正是错误消息的含义:
AttributeError: 'function' object has no attribute 'attribute1'
这是因为items[0]。attribute1
试图访问函数对象上的attribute
,因为items[0]
是函数对象。注:
one_thing = Thing()
for i in range(2):
list_of_things.get_thing(one_thing.give_a_thing)
意识到一件事。给你一件事返回方法本身,你想调用方法:
one_thing = Thing()
for i in range(2):
list_of_things.get_thing(one_thing.give_a_thing())
除此之外,这段代码的结构非常奇怪。为什么give\u thing
只是返回对象本身?这意味着您的事物列表将是一个包含对同一对象的多个引用的列表
你可能想要像这样的东西
class Thing:
def __init__(self, attribute1='y', attribute2='n'):
self.attribute1 = attribute1
self.attribute2 = attribute2
class ThingOfThings:
def __init__(self, items=None):
if items is None: # watch out for the mutable default argument
items = []
self.items = items
def add_thing(self, thing): # use a better name
self.items.append(thing) # don't create a needless intermediate, single-element list
然后简单地说:
list_of_things = ThingOfThings()
for _ in range(2): # style tip: use _ if iterator variable is not used
list_of_things.add_thing(Thing()) # create *new* Thing each iteration
print(list_of_things.items[0].attribute1)
因此,根本问题正是错误消息的含义:
AttributeError: 'function' object has no attribute 'attribute1'
这是因为items[0]。attribute1
试图访问函数对象上的attribute
,因为items[0]
是函数对象。注:
one_thing = Thing()
for i in range(2):
list_of_things.get_thing(one_thing.give_a_thing)
意识到一件事。给你一件事返回方法本身,你想调用方法:
one_thing = Thing()
for i in range(2):
list_of_things.get_thing(one_thing.give_a_thing())
除此之外,这段代码的结构非常奇怪。为什么give\u thing
只是返回对象本身?这意味着您的事物列表将是一个包含对同一对象的多个引用的列表
你可能想要像这样的东西
class Thing:
def __init__(self, attribute1='y', attribute2='n'):
self.attribute1 = attribute1
self.attribute2 = attribute2
class ThingOfThings:
def __init__(self, items=None):
if items is None: # watch out for the mutable default argument
items = []
self.items = items
def add_thing(self, thing): # use a better name
self.items.append(thing) # don't create a needless intermediate, single-element list
然后简单地说:
list_of_things = ThingOfThings()
for _ in range(2): # style tip: use _ if iterator variable is not used
list_of_things.add_thing(Thing()) # create *new* Thing each iteration
print(list_of_things.items[0].attribute1)
与问题无关,但每次调用构造函数时,项
的默认参数中的[]
都是同一个实例。与问题无关,但可能是下一个需要修复的bug,所以请注意。与问题无关,但[]
在项的默认参数中,每次调用构造函数时
都是同一个实例。与问题无关,但可能是下一个需要修复的错误,所以请注意。谢谢!调用该方法对我很有效。至于代码的结构,这是一个非常粗略的版本,任务实际上已经预先定义了。当我制作我的版本时,我没有想太多。再次感谢!非常感谢。调用该方法对我很有效。至于代码的结构,这是一个非常粗略的版本,任务实际上已经预先定义了。当我制作我的版本时,我没有想太多。再次感谢!