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 trial

Python

Why do you use self.start_time in the summary method but NOT self.question_start in the ask method?

In this video I don't understand why you need to reference it as being self.start_time if it isn't an attribute of the class.

What challenge or video?

Brendan Whiting - Updated the question to include the link to the video. Minute 1:57 :)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Kenneth uses the phrase "We need to remember to hold on the that...." at various points in the video. To hold on to something, that is, make it persistent, it needs to be assigned to an attribute. In the ask() method, question_start is not needed beyond the return of the method so it's defined as a local variable.

In the summary() method, he does want to hold on to it, so he makes it persistent by making it an attribute using "self."

He could have used self.question_start but it's best to use a local variable if possible.

Chris Freeman, thanks for your answer! Let me double check if I get it: essentially by making it self.variable_name you're making it a global variable for that class?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

César, "global variable" is too broad of a term. It's more like a "local variable" (known as an attribute) that is bound to a class instance. A true local variable in a class object is typically a variable that is not an class attribute such as an intermediate result in a method. All methods within that class instance can reference these attribute using the self. prefix which represents the current instance of that class.