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"]
I'm fairly new to OOP, but does this not do what you want quite nicely? name_list
is a class variable, and every time you create a new Student
object, it gets added to Student.name_list
. Say for example you had a method cheat(self)
which you wanted to perform on the third student, you could run Student.name_list[2].cheat()
. Code:
class Student():
name_list = []
def __init__(self, score, gender):
Student.name_list.append(self)
self.score = score
self.gender = gender
#this is just for output formatting
def __str__(self):
return "Score: {} || Gender: {}".format(self.score, self.gender)
#again for output formatting
def update(): print([str(student) for student in Student.name_list])
update()
Student(42, "female")
update()
Student(23, "male")
update()
Student(63, "male")
Student(763, "female")
Student("over 9000", "neutral")
update()
Output:
[]
['Score: 42 || Gender: female']
['Score: 42 || Gender: female', 'Score: 23 || Gender: male']
['Score: 42 || Gender: female', 'Score: 23 || Gender: male', 'Score: 63 || Gender: male', 'Score: 763 || Gender: female', 'Score: over 9000 || Gender: neutral']
Did you try your code above? It should work fine. You can condense it into:
scores = [ student.name for student in names if student.gender == "Male" ]
Note that calling the list names
is misleading, since it is a list of Student
instances.
You can't define the list to be a list of Student instances; that's not how Python works.
Are you asking how to create the list that you've called names
?
names = [ ]
for ( score, gender ) in <some-data-source>:
names.append( Student( score, gender ) )
which is of course equivalent to
names = [ Student( score, gender ) for score, gender in <some-data-source> ]
and in turn to
names = [ Student( *row ) for row in <some-data-source> ]
If you need to do a lot of processing for each row then you can either move the processing into a separate function or use a for
loop.
def process_row( row ):
...
return score, gender
names = [ Student( *process_row( row ) ) for row in <some-data-source> ]
Responding to your edit, I think you are trying to declare the types of variables in Python. You wrote:
for i in range(len(names)):
student = Student()
student = names[i]
if student.gender == "Male":
# Whatever
What is the purpose of the line student = Student()
-- are you trying to declare the type of the variable student
? Don't do that. The following will do what you intended:
for student in students:
if student.gender == "Male":
# Whatever
Notice several things:
range(n)
and then look up each instance in names
; iterating over every element of a container is the purpose of a for
loop.student
is -- it could be a string, a boolean, a list, a Student
, whatever. This is dynamic typing. Likewise, students
doesn't have to be a list; you can iterate over any iterable.student.gender
, Python will get the gender
attribute of student
, or raise an exception if it doesn't have one.