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 trialBrian Pitts
Courses Plus Student 3,826 Pointsproper way to access properties of objects and classes in python
What is the proper way to access properties of on instances and classes in python? In other words, are getters and setters encouraged in python or is it "proper" to simply call the properties of an object as in object.propery()?
2 Answers
Kenneth Love
Treehouse Guest TeacherGetters and setters are fairly uncommon in Python. If the attribute or property exists and you need the value, just use it. If you need to set an attribute (you can't set properties by default), just set it.
Brian Pitts
Courses Plus Student 3,826 PointsThanks Kenneth. I can see the Java programmers reeling on this one.... encapsulation! I like the inherent simplicity though.
P.S. Enjoying the Python courses!
Kenneth Love
Treehouse Guest TeacherWell, you can still encapsulate data if you need to. Most of the time, though, your objects are only exposing things you want used. Python has a strong mentality of "we're all adults here".
Alexander Hansson
2,083 PointsAlexander Hansson
2,083 PointsWhat are getters and setters? Could you please explain? :) Thank you.
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherIn other languages, you often have to define methods on your objects for how attributes are retrieved (by getters) and set (by setters).