[英]Why is this multi-value data structure not working?
I'm learning about Python data structures, and am looking at this code:我正在学习 Python 数据结构,并且正在查看这段代码:
class Student:
firstname=""
lastname=""
grade="U"
list=[]
student=Student()#create the student
student.firstname=str(input("Input student first name: "))
list.append(student.firstname)
student.lastname=str(input("Input student last name: "))
list.append(student.lastname)
student.grade=str(input("Input student grade: "))
list.append(student.grade)
#print the student data
print("First Name: " + student.firstname)
print("Last Name: " + student.lastname)
print("Grade: " + student.grade)
After the first input for student.firstname
, it just says there's an error ( TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'str' object
)在第一次输入
student.firstname
之后,它只是说有一个错误( TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'str' object
)
What's wrong?怎么了? I thought the code would just add the firstname to the end of the list, right?
我以为代码只会将名字添加到列表的末尾,对吗?
The name list
refers to the built-in type.名称
list
指的是内置类型。 list.append
is an unbound method of that type, and has nothing to do with the class attribute named list
associated with the class Student
. list.append
是该类型的未绑定方法,与与 class Student
关联的名为list
的 class 属性无关。
It would "work" if you passed the appropriate list as the first argument (for reasons I won't get into here)如果您将适当的列表作为第一个参数传递,它将“起作用”(由于我不会在这里讨论的原因)
list.append(student.list, student.firstname)
but you should really be defining additional methods for the class, in addition to the __init__
method that should define the attributes in the first place.但是除了应该首先定义属性的
__init__
方法之外,您实际上应该为 class 定义其他方法。
class Student:
def __init__(self, firstname, lastname, grade):
self.firstname = firstname
self.lastname = lastname
self.grade = "U"
x = input("Input student first name: ")
y = input("Input student last name: ")
z = input("Input student grade: ")
student = Student(x, y, z)
print("First Name: " + student.firstname)
print("Last Name: " + student.lastname)
print("Grade: " + student.grade)
It's not clear what the purpose of the list is, anyway.无论如何,尚不清楚该列表的目的是什么。 Do you want to keep a list of instances of
Student
, perhaps?你想保留
Student
的实例列表吗? Do that outside the class. For example,在 class 之外执行此操作。例如,
students = []
student = Student("John", "Doe", "U")
students.append(student)
list=[]
and list.append(student.firstname)
are one problem: list
is a type
, not an appropriate object name list=[]
和list.append(student.firstname)
是一个问题: list
is a type
, not an appropriate object name
I'd recommend backing up to basics and implementing:我建议回到基础并实施:
def __init__(self, first, last, grade)
def __init__(self, first, last, grade)
创建您的 class 个实例__str__(self)
to define how to print()
your objects __str__(self)
的定义来定义如何print()
你的对象list
, eg allStudents = []
list
,例如allStudents = []
allStudents.append(Student(first, last, grade))
allStudents.append(Student(first, last, grade))
ref: https://www.w3schools.com/python/python_classes.asp参考: https://www.w3schools.com/python/python_classes.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.