Python-使用对象列表,属性错误
我有一个Python-使用对象列表,属性错误,python,list,Python,List,我有一个orc对象和一个radar对象,需要跟踪它能看到的orcs。我正在使用列表跟踪orcs,但当我试图从列表中拉出orc(或关于它的数据)时,我得到一个属性错误 确切的错误是: AttributeError: type object 'orc' has no attribute 'name' 这是雷达课 from source.orc import orc class radar(object): """ radar Class """ def __ini
orc
对象和一个radar
对象,需要跟踪它能看到的orc
s。我正在使用列表跟踪orc
s,但当我试图从列表中拉出orc
(或关于它的数据)时,我得到一个属性错误
确切的错误是:
AttributeError: type object 'orc' has no attribute 'name'
这是雷达课
from source.orc import orc
class radar(object):
"""
radar Class
"""
def __init__(self, maxOrcs=25):
self.maxOrcs = maxOrcs
self.numOrcs = 0
self.orcList = [orc]
logger.info('Radar created!')
def addOrc(self, orc=None):
self.numOrcs = self.numOrcs +1
self.orcList.append(orc)
logger.info('Orc added to battlefield!')
def distance(self, name):
retVal = None
logger.debug('calculating distance')
for orc in self.orcList:
if name in orc.name:
retVal = ( (500 - orc.locx) + (500 - orc.locy) )
break
return retVal
如果有帮助的话,这是兽人的职业
class orc(object): #Orc class
def __init__(self, name='Krusk', locx=1, locy=1, speed=1):
#set name
if(len(name) < 20):
self.name = name
else:
self.name = 'Krusk'
logger.warning('Default name used')
#set x coord
if(isinstance(locx, int) != True):
self.locx = 1
logger.warning('x coordinate set to 1, Not an int')
elif(locx < 1):
self.locx = 1
logger.warning('x coordinate set to 1, passed value to low')
elif(locx > 1000):
self.locx = 1000
logger.warning('x coordinate set to 1000, passed value to high')
else:
self.locx = locx
#set y coord
if(isinstance(locy, int) != True):
self.locy = 1
logger.warning('x coordinate set to 1, Not an int')
elif(locy < 1):
self.locy = 1
logger.warning('y coordinate set to 1, passed value to low')
elif(locy > 1000):
self.locy = 1000
logger.warning('x coordinate set to 1000, passed value to high')
else:
self.locy = locy
#set speed
if(speed < 0):
self.speed = 0
logger.warning('speed set to 0, passed value cannot be negative')
else:
self.speed = speed
logger.info('Orc created!')
类orc(对象):#orc类
定义初始化(self,name='Krusk',locx=1,locy=1,speed=1):
#设置名称
如果(len(name)<20):
self.name=名称
其他:
self.name='Krusk'
logger.warning('使用默认名称')
#集合x坐标
如果(isinstance(locx,int)!=True):
self.locx=1
logger.warning('x坐标设置为1,而不是int')
elif(locx<1):
self.locx=1
logger.warning('x坐标设置为1,传递值为低')
elif(locx>1000):
self.locx=1000
logger.warning('x坐标设置为1000,传递值为高')
其他:
self.locx=locx
#设置y坐标
如果(isinstance(locy,int)!=True):
self.locy=1
logger.warning('x坐标设置为1,而不是int')
elif(locy<1):
self.locy=1
logger.warning('y坐标设置为1,传递值为低')
elif(locy>1000):
self.locy=1000
logger.warning('x坐标设置为1000,传递值为高')
其他:
self.locy=locy
#设定速度
如果(速度<0):
自身速度=0
logger.warning('速度设置为0,传递值不能为负值')
其他:
自身速度=速度
logger.info('Orc已创建!')
您没有在此处创建orc
类的实例:
self.orcList = [orc]
您在那里使用了类,orc
类没有name属性。也许您想创建一个实例:
self.orcList = [orc()]
您的代码还使用orc
作为类名称和局部变量(在循环中或作为函数参数),这增加了混淆的可能性
您应该为类使用CamelCase命名来避免这种情况;见:
及
创建实例将变成:
self.orcList = [Orc()]
尽管
self.orc\u列表
会更好,但如果您仍然遵循样式指南。您没有在此处创建orc
类的实例:
self.orcList = [orc]
您在那里使用了类,orc
类没有name属性。也许您想创建一个实例:
self.orcList = [orc()]
您的代码还使用orc
作为类名称和局部变量(在循环中或作为函数参数),这增加了混淆的可能性
您应该为类使用CamelCase命名来避免这种情况;见:
及
创建实例将变成:
self.orcList = [Orc()]
尽管
self.orc\u list
会更好,但如果您仍然遵循样式指南的话。我同意其他人的观点,这一行代码就是问题所在:
self.orcList = [orc]
一、 然而,在如何解决这一问题上,他们并不同意。我想你不想在你的初始值设定项中列出任何东西。我认为您希望创建一个空列表:
self.orcList = []
当您调用addOrc()添加条目时,它将准备好接受条目。这与在初始值设定项中将计数设置为0一致。最有意义的是,您的初始值设定项没有收到要添加到列表中的orc对象
编辑:
因为您按名称从列表中检索,您可能会考虑为存储而不是按列表使用按名称键入的DICT。dict将允许您直接访问所需的orc,而不必遍历整个列表,直到找到所需的orc
self.orclist = {}
self.orclist[orc.name] = orc
chosen = self.orclist[name]
# do whatever with chosen
而不是:
for orc in self.orclist:
if orc.name == name:
# do whatever
我同意其他人的观点,这一行代码就是问题所在:
self.orcList = [orc]
一、 然而,在如何解决这一问题上,他们并不同意。我想你不想在你的初始值设定项中列出任何东西。我认为您希望创建一个空列表:
self.orcList = []
当您调用addOrc()添加条目时,它将准备好接受条目。这与在初始值设定项中将计数设置为0一致。最有意义的是,您的初始值设定项没有收到要添加到列表中的orc对象
编辑:
因为您按名称从列表中检索,您可能会考虑为存储而不是按列表使用按名称键入的DICT。dict将允许您直接访问所需的orc,而不必遍历整个列表,直到找到所需的orc
self.orclist = {}
self.orclist[orc.name] = orc
chosen = self.orclist[name]
# do whatever with chosen
而不是:
for orc in self.orclist:
if orc.name == name:
# do whatever
它应该是:
self.orcList=[orc()]
您正在附加类型对象,而不是orc实例。请尝试追加orc()
。它应该是:self.orcList=[orc()]
您追加的是类型对象,而不是orc的实例。请尝试附加orc()
。谢谢您的帮助!这样做更有意义,而且解决了我的问题。我还将按照你的建议看一下样式指南。谢谢你的帮助!这样做更有意义,而且解决了我的问题。我还将按照您的建议看一下样式指南。