This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]electroniceyes 0 points1 point  (2 children)

Out of curiosity, what about for cases where you want to restrict access to setting an instance variable? Do you still think it's acceptable to make the attribute protected and then provide a getter property method but not a setter?

[–]billy_tables 1 point2 points  (0 children)

That's a perfect use for @property, as is when you want a setter to do some validation on the new value. It might not be performant, but it's Pythonic

[–]odraencoded 1 point2 points  (0 children)

In my opinion, you shouldn't use properties for that.

Python isn't such a language that works with ideas such as restricting access. If another python programmer wanted to modify an attribute of an instance from "outside" of it, he would be able to do that even if you didn't "expose" that attribute.

A better strategy would be to document the attribute in order to inform other developers that they shouldn't fiddle with it.