如何在 Python 类中创建对象列表
我们可以通过在列表中添加类实例来创建 Python 中的对象列表。这样,列表中的每个索引都可以指向类的实例属性和方法,并且可以访问它们。如果仔细观察,对象列表的行为就像 c 语言中的结构数组。让我们借助示例来更好地理解它。
示例#1:
# Python3 code here creating class
class geeks:
def __init__(self, name, roll):
self.name = name
self.roll = roll
# creating list
list = []
# appending instances to list
list.append( geeks('Akash', 2) )
list.append( geeks('Deependra', 40) )
list.append( geeks('Reaper', 44) )
for obj in list:
print( obj.name, obj.roll, sep =' ' )
# We can also access instances attributes
# as list[0].name, list[0].roll and so on.
输出:
Akash 2
Deependra 40
Reaper 44
版权属于:月萌API www.moonapi.com,转载请注明出处