Python 如何从类中创建列表?
假设我有一个名为Books的类,其中包含变量:author、title和book_id。然后我还有另一个类名Patrons,其中包含变量:name、patron_id和borroweds。borroweds应该是当前“签出”的书籍列表,因此我必须将类书合并到类patron中。但是我该怎么做呢 这就是我到目前为止所做的:Python 如何从类中创建列表?,python,function,class,Python,Function,Class,假设我有一个名为Books的类,其中包含变量:author、title和book_id。然后我还有另一个类名Patrons,其中包含变量:name、patron_id和borroweds。borroweds应该是当前“签出”的书籍列表,因此我必须将类书合并到类patron中。但是我该怎么做呢 这就是我到目前为止所做的: class Book: author= "" title = "" book_id= "" # the class constructor
class Book:
author= ""
title = ""
book_id= ""
# the class constructor
def __init__(self, author, title, book_id):
self.title = title
self.author = author
self.book_id = book_id
def __str__(self):
s = str("Books("+str(self.author)+", "+str(self.title)+", "+str(self.book_id+")"))
return s
def __repr__(self):
return str(self)
class Patron:
name= ""
patron_id= ""
borroweds= list()
# the class constructor
def __init__(self, name, patron_id, borroweds):
self.name= name
self.patron_id= patron_id
self.borroweds= borroweds
def __str__(self):
s= str("Patron("+str(self.name)+","+str(self.patron_id)+","+list(self.borroweds)+")")
return s
def __repr__(self):
return str(self)
另外,跳过类属性,您不需要它们
class Book:
# the class constructor
def __init__(self, author, title, book_id):
self.title = title
self.author = author
self.book_id = book_id
def __str__(self):
s = str("Books("+str(self.author)+", "+str(self.title)+", "+str(self.book_id+")"))
return s
def __repr__(self):
return str(self)
class Patron:
# the class constructor
def __init__(self, name, patron_id, borroweds):
self.name= name
self.patron_id= patron_id
self.borroweds= borroweds
def __str__(self):
s= str("Patron("+str(self.name)+","+str(self.patron_id)+","+str(self.borroweds)+")")
return s
def __repr__(self):
return str(self)
另外,跳过类属性,您不需要它们
class Book:
# the class constructor
def __init__(self, author, title, book_id):
self.title = title
self.author = author
self.book_id = book_id
def __str__(self):
s = str("Books("+str(self.author)+", "+str(self.title)+", "+str(self.book_id+")"))
return s
def __repr__(self):
return str(self)
class Patron:
# the class constructor
def __init__(self, name, patron_id, borroweds):
self.name= name
self.patron_id= patron_id
self.borroweds= borroweds
def __str__(self):
s= str("Patron("+str(self.name)+","+str(self.patron_id)+","+str(self.borroweds)+")")
return s
def __repr__(self):
return str(self)
另外,跳过类属性,您不需要它们
class Book:
# the class constructor
def __init__(self, author, title, book_id):
self.title = title
self.author = author
self.book_id = book_id
def __str__(self):
s = str("Books("+str(self.author)+", "+str(self.title)+", "+str(self.book_id+")"))
return s
def __repr__(self):
return str(self)
class Patron:
# the class constructor
def __init__(self, name, patron_id, borroweds):
self.name= name
self.patron_id= patron_id
self.borroweds= borroweds
def __str__(self):
s= str("Patron("+str(self.name)+","+str(self.patron_id)+","+str(self.borroweds)+")")
return s
def __repr__(self):
return str(self)
另外,跳过类属性,您不需要它们
class Book:
# the class constructor
def __init__(self, author, title, book_id):
self.title = title
self.author = author
self.book_id = book_id
def __str__(self):
s = str("Books("+str(self.author)+", "+str(self.title)+", "+str(self.book_id+")"))
return s
def __repr__(self):
return str(self)
class Patron:
# the class constructor
def __init__(self, name, patron_id, borroweds):
self.name= name
self.patron_id= patron_id
self.borroweds= borroweds
def __str__(self):
s= str("Patron("+str(self.name)+","+str(self.patron_id)+","+str(self.borroweds)+")")
return s
def __repr__(self):
return str(self)
你注意到书的“str”方法中的打字错误了吗?结尾处的括号需要向左移动,在
self.book\u id
之后
您不需要类属性,因为它们用于每个“用户”的“全局”目的。因此,如果您想跟踪您的用户数量,您可以在每次创建一个“全局”变量时更新该变量,如下所示:
class Patron:
patron_id= 0
# the class constructor
def __init__(self, name, borroweds):
self.name= name
self.patron_id=self.patron_id
self.borroweds= borroweds
每次创建用户对象时,都可以将其添加到类属性中:
p1 = Patron('Dave',[Book('Me','Something', '8675309')])
print p1.patron_id
Patron.patron_id+=1
p2 = Patron('Dave',[Book('You','Others', 'Number9')])
print p2.patron_id
您会注意到class属性已更改,并设置了objects属性。您甚至可以在Patron
中创建一个class属性,该属性是每个Patron
对象的列表,如果需要,可以在_uinit__;方法中添加每个对象。它会在课堂上记录下来
另外,我认为你需要
,“+list(self.borroeds)+”
成为,“+str(self.borroeds)+”
你注意到图书的“str”方法中的打字错误了吗?结尾处的括号需要向左移动,在self.book\u id
之后
您不需要类属性,因为它们用于每个“用户”的“全局”目的。因此,如果您想跟踪您的用户数量,您可以在每次创建一个“全局”变量时更新该变量,如下所示:
class Patron:
patron_id= 0
# the class constructor
def __init__(self, name, borroweds):
self.name= name
self.patron_id=self.patron_id
self.borroweds= borroweds
每次创建用户对象时,都可以将其添加到类属性中:
p1 = Patron('Dave',[Book('Me','Something', '8675309')])
print p1.patron_id
Patron.patron_id+=1
p2 = Patron('Dave',[Book('You','Others', 'Number9')])
print p2.patron_id
您会注意到class属性已更改,并设置了objects属性。您甚至可以在Patron
中创建一个class属性,该属性是每个Patron
对象的列表,如果需要,可以在_uinit__;方法中添加每个对象。它会在课堂上记录下来
另外,我认为你需要
,“+list(self.borroeds)+”
成为,“+str(self.borroeds)+”
你注意到图书的“str”方法中的打字错误了吗?结尾处的括号需要向左移动,在self.book\u id
之后
您不需要类属性,因为它们用于每个“用户”的“全局”目的。因此,如果您想跟踪您的用户数量,您可以在每次创建一个“全局”变量时更新该变量,如下所示:
class Patron:
patron_id= 0
# the class constructor
def __init__(self, name, borroweds):
self.name= name
self.patron_id=self.patron_id
self.borroweds= borroweds
每次创建用户对象时,都可以将其添加到类属性中:
p1 = Patron('Dave',[Book('Me','Something', '8675309')])
print p1.patron_id
Patron.patron_id+=1
p2 = Patron('Dave',[Book('You','Others', 'Number9')])
print p2.patron_id
您会注意到class属性已更改,并设置了objects属性。您甚至可以在Patron
中创建一个class属性,该属性是每个Patron
对象的列表,如果需要,可以在_uinit__;方法中添加每个对象。它会在课堂上记录下来
另外,我认为你需要
,“+list(self.borroeds)+”
成为,“+str(self.borroeds)+”
你注意到图书的“str”方法中的打字错误了吗?结尾处的括号需要向左移动,在self.book\u id
之后
您不需要类属性,因为它们用于每个“用户”的“全局”目的。因此,如果您想跟踪您的用户数量,您可以在每次创建一个“全局”变量时更新该变量,如下所示:
class Patron:
patron_id= 0
# the class constructor
def __init__(self, name, borroweds):
self.name= name
self.patron_id=self.patron_id
self.borroweds= borroweds
每次创建用户对象时,都可以将其添加到类属性中:
p1 = Patron('Dave',[Book('Me','Something', '8675309')])
print p1.patron_id
Patron.patron_id+=1
p2 = Patron('Dave',[Book('You','Others', 'Number9')])
print p2.patron_id
您会注意到class属性已更改,并设置了objects属性。您甚至可以在Patron
中创建一个class属性,该属性是每个Patron
对象的列表,如果需要,可以在_uinit__;方法中添加每个对象。它会在课堂上记录下来
另外,我认为你需要
”、“+list(self.borroeds)+”、“+str(self.borroeds)+”、“+str(self.borroeds)+”、“”、“+str(self.borroeds)+”、还有,这是如何在用户中使用书籍的?@user2829177:你不需要这些属性,因为你不需要这些属性。这就像在做巧克力蛋糕时问自己为什么不需要把一头牛放进冰箱:因为你不需要。@user2829177上面我的代码制作了一个图书列表(承认只有一本书,但它仍然是一个列表),并将其作为赞助人中的借用
属性。我想你可能想得太多了,其实很简单。:-)@user2829177我添加了一个可能会澄清这一点的示例。@user2829177我认为您没有过度思考,我认为您没有意识到类变量和对象变量之间的差异。否则,只需使用用户
的实现并尝试以下操作:打印用户名
。无论您制作了什么对象,都将打印空字符串。现在使用Lennarts代码并尝试相同的方法。它将抛出一个异常,但如果您尝试打印用户名,它将打印用户名,就像Lennart实例化用户对象一样。此外,这如何使用用户中的书籍?@user2829177:您不需要属性,因为您不需要属性。这就像在做巧克力蛋糕时问自己为什么不需要把一头牛放进冰箱:因为你不需要。@user2829177上面我的代码制作了一个图书列表(承认只有一本书,但它仍然是一个列表),并将其作为赞助人中的借用
属性。我想你可能想得太多了,其实很简单。:-)@user2829177我添加了一个可能会澄清这一点的示例。@user2829177我认为您没有过度思考,我认为您没有意识到类变量和对象变量之间的差异。否则很简单