简体   繁体   English

Python属性错误-类型对象没有属性

[英]Python Attribute Error - Type Object has no Attribute

I am writing some code for a project at school. 我在学校为一个项目写一些代码。 I am reading in a List that I have created as a text file with 5 attributes. 我正在阅读一个列表,该列表已创建为具有5个属性的文本文件。 This is my class object code: 这是我的类对象代码:

class studentclass(object):
    def __init__(self,firstname,lastname,classno,correct,mydate):
        self.firstname = firstname
        self.lastname = lastname
        self.classno = classno
        self.correct = correct
        self.mydate = mydate

Later in the program I am using this code to read in the data, sort it and perform some calculations: 在程序的后面,我将使用以下代码读取数据,对其进行排序并执行一些计算:

myList = [studentclass]
totalnoofrecords = 0
counter = 0

for counter in range(0,totalnoofrecords):
    firstname = myList.firstname[counter]
    lastname = myList.lastname[counter]
    classno = myList.classno[counter]
    correct = myList.correct[counter]
    mydate = myList.mydate[counter]
    newname = myList.firstname[counter +1]
    if newname == firstname:
            grade = grade + studentclass.correct(counter +1)
            nummberofattempts = 2
    newname2 = studentclass.firstname(counter +2)
    if newname2 == firstname:
            grade = grade + studentclass.correct(counter +2)
            nummberofattempts = 3
    mean = grade / numberofattempts

    print ("num ",counter ,"=", myList[counter])

But it does not work. 但这行不通。 I get the following error message: 我收到以下错误消息:

AttributeError: 'list' object has no attribute 'firstname' AttributeError:“列表”对象没有属性“名字”

The error message points to this line of the code: 错误消息指向代码的这一行:

firstname = myList.firstname[counter] 名字= myList.firstname [计数器]

Hoping that someone call help me please. 希望有人打电话帮助我。 Thanks 谢谢

In your code you are referencing mylist.firstname . 在您的代码中,您引用的是mylist.firstname What is mylist ? 什么是我的mylist It's a list. 这是一个清单。 Does it have a firstname attribute? 它具有firstname属性吗? The error is telling you that it doesn't, and looking at the code you aren't adding that attribute to the list. 错误是告诉您它没有,并且您没有在代码中将该属性添加到列表中。

Each element of the list has that attribute, however. 但是,列表的每个元素都具有该属性。 Perhaps you meant to get the firstname attribute of one of the elements of the list. 也许您打算获取列表元素之一的firstname属性。 Maybe the following, perhaps? 也许以下,也许?

for counter in range(0,totalnoofrecords):
    firstname = myList[counter].firstname
    lastname = myList[counter].lastname
    ...

In python, when you get an error like "object X has no attribute Y", you can usually rely on that being a true statement. 在python中,当出现诸如“对象X没有属性Y”之类的错误时,通常可以将其视为真实语句。 So, ask youself "why does X not have that attribute?". 因此,问自己“为什么X不具有该属性?”。 It's usually either a) you forgot to define that attribute, b) you misspelled the attribute or you misspelled X, or c) X isn't what you think it is. 通常是a)忘记定义该属性,b)拼写错误的属性或X拼写错误,或者c)X不是您认为的那样。

You have several issues. 你有几个问题。 As Alex S. pointed out, your myList is a List, and specifically it is a list with one element: a class constructor. 正如Alex S.指出的那样,您的myList是一个List,特别是一个包含一个元素的列表:类构造函数。
I think what you want is something like: 我认为您想要的是这样的:

  # assumption: you have textlines, 
  # which is an array of lines of the form firstname,lastname,blah
  myList = [studentclass(*(args.split(",")) for args in textlines]

And then do myList[counter].firstname to get the (counter-th) firstname value 然后执行myList[counter].firstname以获取(第myList[counter].firstname个)firstname值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM