0

I and my friend debated which prefix is a convention and the preferred way to use when you create attributes to a class and want to tell that they are private. We know there isn't a real private modifier in python, and in both prefixes, _ and __ every programmer will know how to get/set to the attributes anyway.

However, what will be the best way to tell others python programmers that the attribute is private? _ or __?

Eladtopaz
  • 1,036
  • 1
  • 6
  • 21
  • There is no debate, it is a *single underscore*. Double-underscores are for *double-underscore name-mangling*. – juanpa.arrivillaga Aug 19 '21 at 20:59
  • Does https://stackoverflow.com/questions/7456807/python-name-mangling answer your question? – Karl Knechtel Aug 19 '21 at 21:00
  • Double-underscore name-mangling is there to *prevent accidental name collisions in subclasses* for classes that are *meant to be subclassed*. Straight from PEP8: "Generally, double leading underscores should be used only to avoid name conflicts with attributes in classes designed to be subclassed". See: https://www.python.org/dev/peps/pep-0008/#designing-for-inheritance – juanpa.arrivillaga Aug 19 '21 at 21:04
  • Thank you all for the responses – Eladtopaz Aug 19 '21 at 21:05

0 Answers0