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 Solution

Lori Miller
Lori Miller
3,666 Points

Different Solution to OOP Vocabulary

class Treehouse:
    product = str('coding courses')

def __init__(self, name):
    self.name = name

def learn(self):
    return (f'{self.name} is learning to code!')

my_treehouse = Treehouse()
my_treehouse.name = 'Lori'

print(learn(my_treehouse))

I was able to get the same output as Megan, but with different code. After looking at the solution, I realized that I didn't have to use the str constructor, but the main thing I noticed was my lack of indentation. Ultimately, aside from the unneeded constructor, I'm wondering if my code would still be considered "Pythonic".

The importance of indentation in Python is something I still struggle to understand and I'm wondering if anyone has any resources that they could share.

1 Answer

Steven Parker
Steven Parker
240,995 Points

Anything between quotes is automatically a string.

While this code might output the same thing as the video example, what it is doing is very different. And yes, indentation is critical in Python. In this case, it makes the difference between defining global functions or creating class methods. That's why you end up calling the function directly instead of using the dot notation to access a method.

And creating global functions and class methods are both common practices, but having a global function named __init__ is definitely not (nor would it be considered "Pythonic").   :blush: