I create multiple objects of a class Student, by using a loop and saving them in the list object_list.
Now I want to create a function, which gives me back one specific attribute of a certain object from the object list. For example the attribute "name" of the third created object.
Perhaps it is possible to do this with the getattr() function, but the problem is that every created object is called the same ("Student").
Is there a way to index the object description "Student" while creating them in the loop, like Student_1, Student_2, Student_3?
This is my code in which I create the objects:
# Definition of the class Student
class Student(object):
name = ""
degree = ""
grade = ""
def __init__(self, name, degree, grade):
self.name = name
self.degree = degree
self.grade = grade
object_list = []
# Create objects
def create_objects(data):
x = 0
# Loop through lists of data
for list in data[1:]:
x = x + 1
# Get values from data to instantiate objects
object_list.append(Student(get_value(data, x, 0), get_value(data, x, 1), get_value(data, x, 2))
return object_list
Now I want to create a function I can use like this:
get_attribute(object_list, "Student_3", "name")
How do I call a specific object from the object_list?
data, which makes it hard to provide a complete solution. If the problem is that the names you're getting fromdataare all the same and you want them to be different, that's a different problem from selecting the resulting object that has a particular name.