creating and naming objects
Brian
bnblazer at gmail.com
Wed Jun 7 17:42:06 EDT 2006
Maarten van Veen wrote:
<snip>
Hi Brian,
>
> If you would do it like this:
> Class Student:
> def setName(self, name)
> self.name = name
> def setId(self, id)
> self.id = id
>
>
> def createStudent():
> foo = Student()
> foo.setName("Brian")
> foo = Student()
> print foo.getName()
>
> You would get an error here, because foo now equals the second Student
> object which has no name. And you can't get to the first one.
> Perhaps you could do something like this.
>
> Class Student:
> def setName(self, name)
> self.name = name
> def setId(self, id)
> self.id = id
>
> listWithStudents = []
>
> def createStudent():
> listWithStudent.append(Student())
>
> Now every student you create is put in the list with students so you can
> access them but don't have to give them names.
> You could do something to all of them like this:
> for student in listWithStudent:
> student.doSomething()
>
> Or change just one student:
> listWithStudent[23].doSomething() # this is the 24th student though!!!
>
> Hope this is what you're looking for.
> Maarten
This is of great help. Thank you,
Brian
More information about the Python-list
mailing list