Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python (retired) Objects Create a Class with a Method

Brent Liang
PLUS
Brent Liang
Courses Plus Student 2,944 Points

Bummer

The question specifies that keywords should be passed to .format() if the placeholders have names, what does it mean and what's wrong with the following code?

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

    def hours():
        return "We're open from {} to {}.".format(open,close)

1) Don't use built-in functions name like that open() as a variable name. 2) All methods in class must have 'self' as the first argument. 3) Best way to init variables in class, use special method init. This method runs when you create instances of class every time.

Your code may be like that.

class Store:
    def __init__(self):
        self.start = 9
        self.end = 16

    def hours(self):
        return "We're open from {} to {}.".format(self.start, self.end)

c = Store()
print c.hours()
Steven Parker
Steven Parker
217,626 Points

:point_right: This challenge specifically asks you to use open as a variable name.

Brent Liang
Brent Liang
Courses Plus Student 2,944 Points

Thank you evgenypanov!!

I love your code and it definitely shows how important the "self" parameter is.

Sorry I have to give the best answer to Steven, would've chosen yours!! :)

1 Answer

Steven Parker
Steven Parker
217,626 Points

:point_right: You forgot to include "self" as the method parameter.

You'll also need to use it as a prefix for the class variables in the format.

You might want to review the video on Class Methods.