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 trialHanna Han
4,105 PointsDifferences between Thief().sneaky and Thief.sneaky
Hi,
I wonder what differences are between Thief().sneaky and Thief.sneaky without creating(having) an instance of the class, Thief? Becuase I have got same result with "True" when I run the code in my consol.
I have tired like in the code:
class Thief:
sneaky = True
In my consol:
from characters import Thief
Thief().sneaky = True
Thief.sneaky = True
1 Answer
Tyler B
5,787 PointsThis is kind of a hard question to answer without devolving into talking in circles but I'll give it a shot. When you are calling Thief.sneaky
you are calling the static reference to the constructed class where as when you call Thief().sneaky
you are creating an Anonymous instance of the class. Consider the following examples
Thief().sneaky = False
print(Thief().sneaky)
print(Thief.sneaky)
True
True
Vs.
Thief.sneaky = False
print(Thief().sneaky)
print(Thief.sneaky)
False
False
Anshul Laikar
4,428 PointsAnshul Laikar
4,428 PointsThank you for this!