Given a python class class Student():
and a list names = []
; then I want to create several instances of Student()
and add them into the list names
,
names = [] # For storing the student instances
class Student():
def __init__(self, score, gender):
self.score = score
self.gender = gender
And now I want to check out the scores of all the male students, can I do it like this?
scores = []
for i in names:
if i.gender == "Male":
scores.append(i.score)
My question is: How to create a list that can (if could be done by any statement) store the instance of Student
? Or rather, when I write names = []
, how could I state every element in names
is an instance of Student
so that I can use the attributs of this element despite python is weak type? I hope I made myself clear ;)
Can I write like:
for i in range(len(names)):
student = Student()
student = names[i]
if student.gender == "Male":
# Whatever
I guess not...