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

Andras Andras
Andras Andras
7,941 Points

Kenneth = Thief, is not Kenneth = Thief()

Hello,

I have just found that Kenneth = Thief, is not Kenneth = Thief().

<p>
kenneth = Thief
kenneth.sneaky
True
kenneth.sneaky = False
kenneth.sneaky
False
Thief.sneaky
False 
</p>

I expected that Thief.sneaky will be True. It seem we need to use parenthesis.

<p>
kenneth = Thief()
kenneth.sneaky
True
kenneth.sneaky = False
kenneth.sneaky
False
Thief.sneaky
True
</p>
Steven Parker
Steven Parker
220,415 Points

Something odd happened to your formatting .. it seems to have HTML tags mixed in with the python. Did you add them?

Also if you add the letter "py" after the first 3 backticks, you get syntax coloring which can enhance readability.

1 Answer

Steven Parker
Steven Parker
220,415 Points

When you use the parentheses, you are invoking the class constructor and creating a new instance of that class. Any properties that are part of that instance can be changed without affecting the class itself.

But when you make an assignment without the parentheses, you don't get a new instance but just a new word that references the class directly. So the properties using either word are the properties of the class itself.