1

I am using both Python and C#.

In C# it is almost required to use properties. In Python it is possible to use properties, but I do not see them in use very often.

Is there a specific reason why Pythonistas to not use properties as much as programmers of other languages?

Johan Vergeer
  • 5,208
  • 10
  • 48
  • 105

1 Answers1

3

Python's philosophy

Python is a language for consenting adults. — Alan Runyan Cofounder of Plone

In Python the philosophy is that we're all responsible users (maybe an improvement on the term "consenting adults"), so we do not protect data unless there we believe there to be a benefit for the user.

Cultural evidence:

Reasons we might use properties:

  • Maybe the attribute is something that can be calculated and is not needed often, thus saving space.

  • Maybe the attribute is required to be a specific type that could easily be given the wrong type or value by a user, and so you can create value for the user by managing the access to it.

  • Maybe the attribute would be better as a function, but it started out as a simple dotted lookup but then grew into one of the two above, so we keep the API to avoid breaking users.

Reasons we might not use properties:

  • Maybe the code is only going to be used by the author, making it less important to protect the users.

  • Maybe we are trying to coax more performance from the code by precalculating attributes, and don't mind the memory tradeoffs.

  • Maybe we want our code to be more terse/less verbose.

It's a matter of judgment

Note that the reasons given are matters of the programmer's judgment. Managed properties are not required by the language, but they are a feature. If you think your users don't need them, you can avoid paying the computational costs to do so.

Conclusion

Other languages do not necessarily share this philosophy - instead the culture is that you must write code that you will only use yourself as if you are a community of users. That is why they will have more use of these kinds of data-hiding than you typically see in Python code.

Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331