creating and naming objects

Dennis Benzinger Dennis.Benzinger at gmx.net
Wed Jun 7 12:27:20 EDT 2006


Brian wrote:
> [...]
> For example, lets say that I have a class that creates a student
> object.
> 
> Class Student:
>     def setName(self, name)
>         self.name = name
>     def setId(self, id)
>         self.id = id
> 
> Then I instantiate that object in a method:
> 
> def createStudent():
>     foo = Student()
>     /add stuff
> 
> Now, suppose that I want to create another Student.  Do I need to name
> that Student something other than foo?  What happens to the original
> object?  If I do not supplant the original data of Student (maybe no id
> for this student) does it retain the data of the previous Student
> object that was not altered?  I guess I am asking how do I
> differentiate between objects when I do not know how many I need to
> create and do not want to hard code names like Student1, Student2 etc.
> [...]


Just return your Student object from createStudent() and put it in a 
list. For example:

all_students = []

for i in range(10):
     one_student = createStudent()

     # Do what you want with one_student here

     all_students.append(one_student)

print all_students


BTW: Why don't you use a constructor to create Student objects?

Bye,
Dennis



More information about the Python-list mailing list