Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Queries Are Your Friend

Chih-Wei Hsu
Chih-Wei Hsu
11,132 Points

0.username

what does the 0 refer to in the 0.username?? in the video, kenneth says it's what's returned from the function top_student, but why 0?? does it refer to the index number of the columns in the Student table?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,029 Points

In the line:

        print("Our top student right now is: {0.username}".format(top_student()))

The 0 is the numbered field for the format method. It is populated by the 0-th or first argument to format(). In this case 0 refers to the returned value of top_student(). This is a Student object. The syntax 0.username retrieves the username attribute from the returned Student object. So.... 0.username is the username of the top student.

Chih-Wei Hsu
Chih-Wei Hsu
11,132 Points

Thank you! It makes sense now.

Hi Chris,

I'm not quite sure if I understand it based on your feedback if I can be honest.

I don't understand why 0.username is necessary when all you got back from top_student would be one match being I suppose the first one with the highest score.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,029 Points

Think of 0 as a reference to the object returned by the function call top_student(). In this case, it is the single Student object.

Using 1 as a reference has no meaning here since there isn't a second argument to the format() method.

If top_student() had returned multiple Student objects in a list, then the first student could be referenced as 0[0].username and the second student as 0[1].username, but this is not the case in this example.

Thanks, that makes more sense. But I still feel as I do not have full grasp on it.

As an example: in a situation whereby you'd have a second argument referring to another object, you'd have to pass on 1.name_here, for third argument 2.name_this, and so on?

Imagine (don't mind the length of one line.. :p):

def top_student():
    good_student = Student.select().order_by(Student.points.desc()).get()
    return good_student

def worst_student():
    bad_student = Student.select().order_by(Student.points.desc()).get()
    return bad_student

print("Our top student is {0.username}, our worst is {1.username}!".format(top_student(), worst_student()))

So if I understand it correctly, 0.username refers to the username of the first argument (object top_student), while 1.username refers to the second argument of format being object worst_student.

And we could then extent to add the points of the best student being 0.points and of the worst 1.points in the string as well if I am correct.

edit: corrected some values in the code.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,029 Points

Sorry accidentally hit "best answer" link (it's extremely close to the "add comment" link on an iPhone.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,029 Points

Exactly! That is correct, this syntax shortcut is used if you need to access attributes of the passed argument.

The number syntax "{0}" is simply used to reference a specific argument. It allows to repeatedly use an argument: "{0} {0}" or change the order: "{1} {0}"