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!
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

toirleachomalley
11,971 PointsPrivate Attributes: Does Python implement private attributes?
Kenneth Love, I would think that it is much better practice to implement get and set methods for getting and setting the values of attributes.
E.g. In the Thief class we have an attribute called sneaky. What if at some future point we wish to have a much more complex calculation to define the value of this?
All of the calling code that uses the Thief class will have to be amended whereas, if there is a get method for returning this value then just the method needs to be changed and all of the calling code continues to work oblivious to the change.
What are your thoughts? I'd be interested in hearing the Python take on this? Most people think private attributes are to do with security but I always look at them as a way to help developers use the given methods instead of directly accessing the attributes.
4 Answers

Kenneth Love
Treehouse Guest TeacherThere's no such thing as a private attribute in Python. You can use the property
function/decorator to make a calculated attribute (effectively what you're asking for) but those still aren't private (in the OO sense). For regular ol' attributes, though, it's very non-Pythonic to implement getters and setters. They're not needed (you can set them through the instance already) and it sticks out as code written by someone trying to write Python like it's another language.

toirleachomalley
11,971 Pointsand here I was getting extremely fond of Python. So something that is non-Pythonic seems to be against something that is at the heart of OO (encapsulation).
A bit of a wasted several weeks for me but at least now I know why so many organizations don't see it as an enterprise level language. Thanks

Kenneth Love
Treehouse Guest TeacherYou honestly think there's a single blessed requirements list for OO? OO varies from language to language. Python doesn't have private/protected methods/attributes because it doesn't need them. Everything is public because "we're all adults here". We have plenty of encapsulation in Python, it just doesn't require or recommend access control. In fact, by the time you finish the course, you'll find out about @property
and other methods of encapsulating values and behaviors.
And as for "an enterprise level language", every single major tech player uses Python somewhere in their stack/process. Microsoft, Facebook, Google, Apple, NASA, Tesla, Instagram, Pinterest, YouTube, and on and on and on. Kinda funny that none of them have abandoned Python or dismissed it out of hand because it doesn't have @protected
or something, isn't it?

toirleachomalley
11,971 PointsGetting a bit excited there Kenneth Love :).
See, I read somewhere today that "the best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer". See how passionate you got when I dared offend Python's honor? Don't ya feel alive? At least now that I know there is encapsulation (even if it is non-Pythonic) and that we are all adults (allegedly).

Kenneth Love
Treehouse Guest Teacherlol, it's not about defending Python's honor. I'll let Python do that. It's more about tearing down the idea that there's ever a single best way to do anything in programming. There is no perfect OO system out there. But, sure, if you want to start my day off with a bit of righteous fury, I guess you achieved your goal :D

toirleachomalley
11,971 Pointshaha, these are the types of arguments that make code reviews a lot more fun.