0

A very experienced engineer has told me to NEVER use double underscores for defining methods and variables inside a class, because they are reserved for magic methods and only use a single underscore. I understand that double underscores make attributes private to the class, and a single underscore makes them protected. I also understand that protected attributes is just a mutual understanding between developers. I find it hard to believe to not use private attributes, then why was that concept created in the first place. So my questions are:

  • Is it really bad practice to use double underscores even when it makes sense to make attributes non public?
  • Since protected attributes are "not really protected", wouldn't it make sense to just make it private, because it would have lesser mistakes when done this way?
Cœur
  • 37,241
  • 25
  • 195
  • 267
teddybear123
  • 2,314
  • 6
  • 24
  • 38
  • 3
    This entirely depends on whether you're talking about leading double underscores (e.g. `__foo`) or leading and trailing (e.g. `__foo__`). – miradulo Feb 22 '17 at 16:53
  • @Mitch Just leading double underscore, Not leading and trailing – teddybear123 Feb 22 '17 at 16:54
  • In which case the assertion it will clash with magic methods is false. Magic methods use leading and trailing underscores and therefore can't clash. However here is a different reason not to use them http://stackoverflow.com/questions/6930144/underscore-vs-double-underscore-with-variables-and-methods – Paul Rooney Feb 22 '17 at 16:55
  • @PaulRooney Hi Paul, Question#2 how about the practical usage of protected and private attributes, since private attributes prevent accidental access, wouldn't it make sense to use private all the time instead of having a mutual understanding in protected attributes – teddybear123 Feb 22 '17 at 16:58
  • Hi @JETM that explains the meaning of the scopes, I already conveyed that in my question description. Thank you. – teddybear123 Feb 22 '17 at 16:59
  • For #2 your subclasses will want access to those members. Since the extremely limited nature of the name mangling offered by the double underscore leaves you open to problems with two similarly named subclasses it might be best to avoid it at all costs. – Paul Rooney Feb 22 '17 at 17:03

1 Answers1

1

Here are some corrections to your statements that will hopefully clarify what you are asking about:

Magic methods and attributes are prefixed and suffixed by a double underscore. A double underscore only in the prefix is specifically to make things private.

In Python 3 and above, attributes that are only prefixed with a double underscore get their name mangled to make them more private. You will be unable to access them outside a class using the literal name. This can cause issues outside of classes, so do not use a double-underscore prefix for say module-level attributes: How to access private variable of Python module from class. However, do use them in classes to make things private. If the feature was not intended to be used, it would not have been added to Python.

As far as privacy and protection goes in general, there is no such concept in Python. It is just an expectation that object oriented programmers have coming in from other languages, so there is an established convention for marking attributes as private.

The single underscore prefix is generally the preferred way to mark things as private because it does not mangle the name, leaving privacy at the discretion of the API's user. This sort of privacy/protection is really more of a way to indicate that the attribute is an implementation detail that may change in future versions. There is nothing stopping you from using the attribute, especially if you are OK with your code breaking when it is linked against different versions of libraries.

Keep in mind that even mangled names follow a fixed pattern for a given version of Python. The mangling is intended more to prevent you from accidentally overriding something you didn't intend to than to make attributes truly private. It just adds the class name with a bunch of underscores to your attribute name, so you can still access it directly if you know how.

Here is a good description of pretty much everything I just wrote from the docs: https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264