[Tutor] Something I don't understand

Tiger12506 keridee at jayco.net
Tue Dec 18 15:44:14 CET 2007


> Below, "student_seats" is a list of the class "student".
>
> Why does this code set every student.row to zero when there is only one
> student in the list with row > 5?
> It still sets them all to zero if I change the test to ">200" when there 
> are
> no student.rows > 200.
> But if I change the test to "<1" then nothing gets set to zero.
>
>
> Jim
>
>
> class student:
>     def __init__ (self, name, row, column):
>             self.name = name
>             self.row = row
>             self.column = column
>
>
>
> for student in student_seats:
>        print student.name, "row = ", student.row, "column = ",
> student.column
>                if student.row > 5:
>                        student.row = 0
>         print student.name, "row = ", student.row, "column = ",
> student.column

Probably a naming clash. You have student defined as a class, yet when you 
run the for loop, student is named for each instance of that class in the 
list. Python is most likely getting confused when you say student.row = 0, 
thinking that you mean All student classes' rows set to zero. At any rate, 
it's a bad idea to mix names like that. 



More information about the Tutor mailing list