Should private attributes or public attributes be the default in Python classes?

  softwareengineering

In python we use a leading _ to make object attributes implicitly “private”. If we want to give people set/get access we can use the @property decorator. Or, if setting/getting is allowed and trivial, we can just make the attribute “public” by omitting the leading _.

My colleague and I were debating about two competing philosophies:

A: “my approach in general is to have all attributes private by default. They are escalated to public when we explicitly want the API user to get/set it”

B: “another approach could be to put make attributes private when we explicitly don’t want people to use them, but default to public with the understanding that people know they shouldn’t mess with them”

I don’t have clear question to ask to this community. I’d just like to get thoughts. I’d accept an answer that gives more clarity into how to think of the problem.

LEAVE A COMMENT