Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChih-Wei Hsu
11,132 Points0.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
Treehouse Moderator 68,457 PointsIn 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.
Unsubscribed User
7,260 PointsThanks, 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
Treehouse Moderator 68,457 PointsSorry accidentally hit "best answer" link (it's extremely close to the "add comment" link on an iPhone.
Chris Freeman
Treehouse Moderator 68,457 PointsExactly! 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}
"
Chih-Wei Hsu
11,132 PointsChih-Wei Hsu
11,132 PointsThank you! It makes sense now.
Unsubscribed User
7,260 PointsUnsubscribed User
7,260 PointsHi 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsThink of
0
as a reference to the object returned by the function calltop_student()
. In this case, it is the singleStudent
object.Using
1
as a reference has no meaning here since there isn't a second argument to theformat()
method.If
top_student()
had returned multipleStudent
objects in a list, then the first student could be referenced as0[0].username
and the second student as0[1].username
, but this is not the case in this example.