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 Dates and Times in Python (2014) Let's Build a Timed Quiz App Taking The Quiz

gaetano amoroso
gaetano amoroso
2,993 Points

I have a problem with quiz.py second part, please someone help me

I followed the course oop in python on your portal and I understood the use the self variable, at least I hope. I believe that self in the class context serve to refer at the attributes of the class itself.

the declaration of the attributes are made on the top of the class then are created the methods that serve and we can to refer to them with self.

Howover in the quiz.py above mentioned I saw the self.start_time and self.end_time variable in the take_quiz method , where are they from?

They have not been declared as attributes , not on top up and not in init constructor.

Can I do declaration of an attribute also within a function that isn't constructor?

If is so, what is your scope "visibility"?

How can I refer it in and out the class?

The two variable are scope local within the method that define them?

Maybe aren't they visible at the other methotds whitin the same class?

If I make a instance of a class can I refer them? and if is so, how do I do that?

I think that are a lot questions but I am confused

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Gaetano, let's go over your questions:

"I followed the course oop in python on your portal and I understood the use the self variable, at least I hope. I believe that self in the class context serve to refer at the attributes of the class itself." It would be more correct to say self refers to the attributes of an instance of the class itself.

"the declaration of the attributes are made on the top of the class then are created the methods that serve and we can to refer to them with self." Yes. Additionally, attributes can be created "on the fly" dynamically without any declaration. In fact, no attribute declaration is needed.

"However in the quiz.py above mentioned I saw the self.start_time and self.end_time variable in the take_quiz method , where are they from?". The attributes start_time and end_time are created dynamically upon first assignment.

"They have not been declared as attributes , not on top up and not in init constructor." Correct, declaration is not necessary.

"Can I do declaration of an attribute also within a function that isn't constructor?". Yes, but explicit declaration is not necessary. The first assignment will create the attribute.

"If is so, what is your scope 'visibility'? All variables and attributes are universally visible.

"How can I refer it in and out the class?" In the an instance of the class, the attributes can be referred to with the self. prefix. Out of the class, if you have an instance of Quiz, say my_quiz = Quiz(), you can refer to the attributes as my_quiz.start_time and my_quiz.end_time. Note that in both cases, these attributes will not be available until after the my_quiz.take_quiz() has run.

"The two variable are scope local within the method that define them?" No, because of the self. prefix they become attributes and are universally scoped. If the local variables start_time and end_time had been used (without the self. prefix), then they would only exist during the execution of that method and cease to exist after the method completes.

"Maybe aren't they visible at the other methods within the same class?" Once they are created, all attributes are visible to all methods of the same instance.

"If I make a instance of a class can I refer them? and if is so, how do I do that?" Yes, as above, instance_name.attribute_name

"I think that are a lot questions but I am confused". Not at all. That's why we're here!!

gaetano amoroso
gaetano amoroso
2,993 Points

as always you are very clear and helpful, yours is a great job, thank you very much

Dee K
Dee K
17,815 Points

I just want to say, that is an amazing, in-depth explanation Chris.

Thanks alot for taking the time to explain it!