Given a python class class Student():
and a list names = []
; then I want to create several instances of Student()
and add them into the li
First of all python is not weakly typed. It is however dynamically typed so you can't specify an element type for your list.
However this does not prevent you from accessing an object's attributes. This works just fine:
names = [Student(1,"Male"), Student(2,"Female")]
scores = []
for i in names:
if i.gender == "Male":
scores.append(i.score)
It is however more pythonic to write this using a list comprehension:
names = [Student(1,"Male"), Student(2,"Female")]
scores = [i.score for i in names if i.gender == "Male"]