假设我有一个名为foo
的类,它的定义如下
class foo:
def __init__(self, id):
self.id = id
我希望这一切发生
dummylist = [foo(3), foo(4), foo(10)]
# I want to do that if possible
print(dummylist[10]) # and get the last object in the list
我想要的是,我有一个具有许多属性的类,我想要一个该类的对象列表,并且能够检索具有其属性值的特定对象
我知道我可以通过next
或列表理解来实现这一点,但我想通过用[]
索引对象列表来实现这一点
您可以使用collections.UserList创建自己的列表:
输出:
相关问题 更多 >
编程相关推荐