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

Eoin Kilbride
Eoin Kilbride
4,705 Points

Create Class Challenge

This code won't run- why? In the editor, open is a different color to close which makes me think there's some issue with using the word "open" as an attribute name

class Store: open = 9 close = 5.30

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

Sorry I haven't done the challenge, but I am fairly comfy with Python. I usually define a class like this:

class Store:
    def __init__(self, open, close):
        self.open = open
        self.close = close 

Hope it helps.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Your first Store class passes Task one of the challenge. The second Store class (in comment) does not pass the challenge because it defines instance attributes instead of class attributes. What do you mean by "Won't run"?

The word open is highlighted differently because it is a built-in function. While it could be used as a variable name, using built-in or reserved names as variables is not a good practice. But since the challenge is expecting "open" you're stuck with it. A better choice would be "open_time".

Also, it is customary to put an empty pair of parens after the class name definition. It is recommend in the PEP-0008 style guild to use 4-space indentation.

class Store():  #<-- added parens
    open = 9  #<-- increased indentation
    close = 5.30 #<-- This is 5 and 3/10 hours

Both of your code samples run in the interpreter:

# First Store
>>> class Store:
...   open = 9
...   close = 5.30
... 
>>> my_store = Store()
# Instance dict
>>> my_store.__dict__
{}
# class dict
>>> Store.__dict__
mappingproxy({'open': 9, '__doc__': None, 'close': 5.3, '__dict__': <attribute '__dict__' of 'Store' objects>, '__weakref__': <attribute '__weakref__' of 'Store' objects>, '__module__': '__main__'})

>>> dir(my_store)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'close', 'open']
>>> my_store.open
9
>>> my_store.close
5.3
>>> 
>>> 
# Second Store
>>> class Store:
...     def __init__(self, open, close):
...         self.open = open
...         self.close = close 
... 
>>> other_store = Store(open=8, close=6)
# instance dict
>>> other_store.__dict__
{'open': 8, 'close': 6}
# class dict
>>> Store.__dict__
mappingproxy({'__weakref__': <attribute '__weakref__' of 'Store' objects>, '__doc__': None, '__dict__': <attribute '__dict__' of 'Store' objects>, '__init__': <function Store.__init__ at 0x7f624120d6a8>, '__module__': '__main__'})

>>> dir(other_store)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'close', 'open']
>>> other_store.open
8
>>> other_store.close
6