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 trialRyan Malkmus
2,053 Pointsquestion with using .format() it python
class Store():
open = 9
close = 6
def hours():
return "We're open between {} and {} " .format(self.open, self.close)
I'm not sure if my markdown is going to work. I apologize if it doesn't.
I imagine that I would know how to do this if I watched previous python videos from treehouse. The only python experience I have is from playing around on codecademy. I've never used the .format function yet. Can someone help me learn how to use in this situation?
3 Answers
Jegnesh Gehlot
5,452 PointsRyan, You are almost right. The only thing you are missing is self instance in the method hours. The code should be like this:
class Store():
open = 9
close = 6
def hours(self):
return "We're open between {} and {} " .format(self.open, self.close)
Jegnesh Gehlot
5,452 PointsRyan, Exactly. According to the treehouse a self instance is defined as : "every method that you create on a class takes, at the very least, the self argument. Self always represents the instance that you're calling the method on. But you don't ever have to pass it in yourself, but you do have to write it."
I hope the above statement makes sense. Please let me know if you need further explanation.
Iain Simmons
Treehouse Moderator 32,305 PointsI believe they also mention that you don't actually have to use the word 'self', you just use whatever you pass into the method as the argument.
It's generally considered best practice to use self
, as you suspected. Check out the PEP 8 style guide (also referred to in the Treehouse videos) for more on this and other best practices:
http://legacy.python.org/dev/peps/pep-0008/#function-and-method-arguments
Ryan Malkmus
2,053 PointsRyan Malkmus
2,053 Pointsohhhhh, okay, that makes a lot more sense. Thank you
Ryan Malkmus
2,053 PointsRyan Malkmus
2,053 Pointsis it best practice to use "self" in python like it is to use "this" in JavaScript? Do you know what I mean?