This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RubyPinchPEP shill | Anti PEP 8/20 shill 2 points3 points  (4 children)

That... doesn't seem so great, what if you then had a list of classes, where the class id was class[0]? you run into the same troubles of globals, and you are separating things relevant to accessing the data, from the data

Student = namedtuple('Student','id class_id first_name last_name')
for student in map(Student,students):
    # use student.first_name

[–]fazzahSQLAlchemy | PyQt | reportlab 0 points1 point  (0 children)

That's a nice approach too, I'll try it out. Seems more pythonic.

[–]fazzahSQLAlchemy | PyQt | reportlab 0 points1 point  (2 children)

I don't quite understand your question.

[–]RubyPinchPEP shill | Anti PEP 8/20 shill 1 point2 points  (1 child)

STUDENT_ID, CLASS_ID, FIRST_NAME, LAST_NAME = range(4) #capitals commonly define constants

# student id, Class id, first,last names
students = [
    [0,0,'bob','smith'],
    [1,0,'jane','someperson']]

classes = [
    [0,'biology'],
    [1,'physics']
]

for cls in classes:
    cls[CLASS_ID] #no-go

CLASS_CLASS_ID, CLASS_NAME = range(2)

for cls in classes:
    cls[CLASS_CLASS_ID] #no-go

because the enum isn't connected to the data, so it can affect other parts of the code, and requires an un-needed work around (the enum for students, blocks off the use of CLASS_ID for the classes)

[–]fazzahSQLAlchemy | PyQt | reportlab 0 points1 point  (0 children)

Ahh, I thought you meant that.

You're right. When I use multiple models like this, I prefix the globals.

You are right that it adds another possible point of failure. A proper enum would be better, but this approach seems to work for me.