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 trialAnatoli Melechko
7,935 PointsI really have no idea what could be wrong with this. def hours(): return "We're open from {} to {}.".format(open,c
I guess I have no idea how to ask questions here. This is a bummer. Time to give up.
class Store:
open = 9
close = 10
def hours():
return "We're open from {} to {}.".format(open,close)
2 Answers
David McCarty
5,214 PointsTwo things you're going to need to do is, firstly, you need to pass in "self" as an argument to the hours(): method.
Also, the reason why it's not working is becasue you need to use self.open and self.close in order to reference the attributes of the class.
class Store():
close = 10
open = 9
def hours(self):
return "We are open {} to {}".format(self.open, self.close)
That should do the trick. The reason why you pass in self as an arguemtn is becasue, essentially, you're calling a function when you call "hours()". Since you're calling a function, you need to be pass in the instance of the object you've created (in this instance, Store) so that way you can reference said objects attributes (In this case, open and close).
Essentially, when you pass in self and you type in "self.close" what you are doing is basically saying "Store.close", you are getting the attribute of the passed in object, which is the object that the method is wrapped around
class Store():
close = 10
open = 9
Is around:
def hours(self):
return "We are open {} to {}".format(self.open, self.close)
So, anytime you pass in "self" as an argument, you're essentially passing in the class to that method so it can use it's attributes and other methods.
Hope that helps!
Anatoli Melechko
7,935 PointsThank you. This Python quirk with passing the reference to instance is a bit of mental switch for me.
David McCarty
5,214 PointsYeah it is for me too! I came from Java so it's a bit different logic.
If this solved your problem, feel free to set my answer as best answer so people know it was answered :)