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 Object-Oriented Python (retired) Objects Create a Class with a Method

Milagros Roman
Milagros Roman
1,228 Points

this gives me an error even if this works very well on the workplace

class Store: open = 9 close = 5

def hours(self): print("we are open from {} to {}".format(self.open, self.close))

tienda = Store() tienda.hours()

with this code on my workplace, I get : we are open from 9 to 5" as it is expected but on your challenge workplace it doesn't work :-9 loosing time..

method.py
class Store:
  open = 9
  close = 5

  def hours(self):
    print("we are open from {} to {}".format(self.open, self.close))

tienda = Store()
tienda.hours()

5 Answers

Dan Johnson
Dan Johnson
40,532 Points

hours needs to return a string, not print one. Also make sure your format string exactly matches the the one in the description:

"We're open from {} to {}."

Everything else looks fine (though creating a Store and calling hours is not required).

Milagros Roman
Milagros Roman
1,228 Points

thank you Dan... yupi .. It was so easy the problem was the phrase!!!! And obviously the print

Mila

Rodrigue Loredon
Rodrigue Loredon
1,338 Points

Hi guys, can you tell me why self.open and self.close instead of just open and close.

Dan Johnson
Dan Johnson
40,532 Points

You need self in order to associate the instance of the calling object with the instance variables. Without self they could get confused with variables local to the function.

Rodrigue Loredon
Rodrigue Loredon
1,338 Points

Dan, is the calling object the hours method ?

Dan Johnson
Dan Johnson
40,532 Points

In the case of the code:

tienda = Store()
tienda.hours()

tienda is what is calling hours as it is an instance of the Store class. So any use of self in this particular call will refer to tienda.

Rodrigue Loredon
Rodrigue Loredon
1,338 Points

Awsome, very clear, thank you very much.