- 0
Notifications Mark All Read
- Login
- Get Prime
How to create a list of objects in Python class
How to create a list of objects in Python class:
We can create a list of object in Python by appending class instances/object to list. By this, every index in the list can point to instance attributes and methods of the class and can access them. Let’s see how to create a list of objects in Python class.
Code #1:
#python Program
#Rishikesh
class A(object):
def __init__(self,name,age):
self.name = name
self.age=age
def display(self):
print(self.name,self.age)
lisob=[]
for i in range(5,10):
lisob.append(A(chr(65+i),i))
for obj in lisob:
obj.display()
Output:
F 5
G 6
H 7
I 8
J 9
Login/Signup to comment