简体   繁体   English

Python:包含类成员的列表

[英]Python: list containing members of a class

nop = 0
patron = list
def createnew(firstname, lastname, phone, age, gender):
    name = (lastname.lower() + ", " + firstname.lower())
    patron.append(patrons(name, phone, age, gender))
    nop += 1
class patrons():
number = 1

    def __init__(self, name, phone, age, gender):
        self.name = name
        self.phone = phone
        self.age = age
        self.gender = gender

There's a couple of parts of my code for a program that holds information about library patrons. 我的程序的代码有几个部分,其中包含有关图书馆顾客的信息。 What I want to do is store all the members of the class (patrons) in a list (patron), I know the names are a little confusing, I wasn't really thinking, sorry about that. 我想做的是将类(赞助人)的所有成员存储在一个列表(赞助人)中,我知道名称有些混乱,我并不是真的在想,对此感到抱歉。 The problem which I am encountering is that when I run the createnew function, I receive an error which says "descriptor 'append' requires a 'list' object but received a 'patrons'" I was under the impression that I could store class objects in a list. 我遇到的问题是,当我运行createnew函数时,我收到一条错误消息:“描述符'append'需要一个'list'对象,但收到了'patrons'”。我的印象是我可以存储类对象在列表中。 Am I unable to do this? 我不能这样做吗? If I can do it, what do I have to change? 如果我能做到,我必须改变什么?

patron = list should probably be patron = list() : patron = list可能应该是patron = list()

Calling list.append(1) reproduces an error similar to the one you mention: 调用list.append(1)产生与您提到的错误类似的错误:

In [1]: list.append(1)
TypeError: descriptor 'append' requires a 'list' object but received a 'int'

To understand the meaning of the error message, you might start by reading about how methods are descriptors . 要了解错误消息的含义,您可以先阅读有关方法如何成为描述符的信息 Also note that in Python2 unbound methods such as list.append must be called with an instance of the calling class (eg list ). 还要注意,在Python2中,必须使用调用类的实例(例如list )来调用未绑定的方法(例如list.append )。 But that is mostly irrelevant to fixing your problem unless you are curious about the nature of the error. 但是,除非您对错误的性质感到好奇,否则这与解决问题几乎没有关系。


patron = list makes patron equal to the built-in class list . patron = list使patron等于内置类list patron = list() makes patron an instance of the class list . patron = list()使patron成为类list的实例。

you should setup patron as 您应该将顾客设置为

patron = list()

Unrelatedly, nop += 1 will throw an UnboundLocalError. 无关地, nop += 1将引发UnboundLocalError。 Add global nop inside createnew More info here 加入global nopcreatenew更多信息点击这里

As said before, list should be list() and you should give nop as an argument: 如前所述,list应该是list(),并且应该给nop作为参数:

nop = 0
patron = list()
def createnew(firstname, lastname, phone, age, gender, nop):
    name = (lastname.lower() + ", " + firstname.lower())
    patron.append(patrons(name, phone, age, gender))
    nop += 1

class patrons():
    number = 1
    def __init__(self, name, phone, age, gender):
        self.name = name
        self.phone = phone
        self.age = age
        self.gender = gender

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

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