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 Object-Oriented Python (retired) Objects What Are Objects And Classes?

Brian Pitts
PLUS
Brian Pitts
Courses Plus Student 3,826 Points

proper 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
STAFF
Kenneth Love
Treehouse Guest Teacher

Getters 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.

Alexander Hansson
Alexander Hansson
2,083 Points

What are getters and setters? Could you please explain? :) Thank you.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

In other languages, you often have to define methods on your objects for how attributes are retrieved (by getters) and set (by setters).

Brian Pitts
PLUS
Brian Pitts
Courses Plus Student 3,826 Points

Thanks Kenneth. I can see the Java programmers reeling on this one.... encapsulation! I like the inherent simplicity though.

P.S. Enjoying the Python courses!

more...

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Well, 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".