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

Michael Steinman
Michael Steinman
11,012 Points

task 2 of 2

I'm stuck on the second assignment for the oop class. I'm instructed to create a class called Store with the attributes "open" and "close" set to particular numbers. I've done this. Then I need to create a method called hours that returns the string "We're open from {} to {}", where open and close are to be inserted using .format. I have no idea how to do this.

I know how to use .format and create functions outside of classes, but within a class, I have no idea. I'm also not sure what this 'self' parameter is all about either.

John Lindsey
John Lindsey
15,641 Points

Did you remember to put self as the parameter of the method? Or put self before "open" and "close" in the parentheses of format? (Sorry about the formatting. When I posted it put the class on one line and didn't include it in the code box, and I'm not sure how to fix it.)

class Store: open = 8 close = 9

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

Got here a little late, Id follow up with Haider he is spot on.

3 Answers

Haider Ali
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Haider Ali
Python Development Techdegree Graduate 24,728 Points

Hi there, any function that is defined within a class is known as a method. The self argument is passed in to methods to allow them to use attributes which belong to their class. For example, you would format in self.open and self.close to the string in task 2 in order to pass it.

class Store:
  open = 4
  close = 5

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

Classes can be slightly difficult at first, but your understanding will increase the more you familiarize yourself with them. If you have any further questions, please feel free to leave a comment :)

Thanks,

Haider

John Lindsey
John Lindsey
15,641 Points

Just realized I made my response a comment instead of an answer. Here is it again, but it's been answered above now.

class Store:
    open = 8
    close = 9

    def hours(self):
        return "We're open from {} to {}.".format(self.open, self.close)
Michael Steinman
Michael Steinman
11,012 Points

Thanks for the responses. I'm able to make it work thanks to your input.

I tried using open and close as parameters in the method and that didn't work. I also tried using self as the parameter and then using open and close in the format. I didn't think to use self as the parameter and self.open and self.close in the format.

The video lectures didn't explain this. Thanks again.