1

I'm trying to understand why it is appropriate to have a method prefaced by __ underscores when no subclasses are present. There are thus technically no naming conflicts that could arise.

I'm specifically referring to the __series_type method in this pvcaptest Github repo. Other methods in the class in this format __get_poa_col and __comb_trans_keys also just get called internally by other methods.

The CapData() class has no subclasses. Is the __series_type prefaced with a double-underscore merely a way to indicate that this is an internal method used only by other methods in the class? Is this even the correct usage of this syntax?

If it is, is it essentially a substitute for using the @staticmethod decorator?

6
  • 2
    Just because you don't define any subclasses doesn't mean that someone else won't.
    – D Stanley
    Commented Oct 21, 2021 at 16:19
  • 1
    By that logic, we should preface every single method that could potentially be used in a slightly different way in a subclass with __? Commented Oct 21, 2021 at 17:57
  • @DStanley, that's pure YAGNI in action there.
    – David Arno
    Commented Oct 22, 2021 at 7:42
  • 1
    It's hard to tell what the author was thinking specifically, but my understanding is that this is just a way to attempt at declaring private functions. There are two conventions in Python for naming a method which is not intended to be called by the clients of your class: a single _ as a soft mechanism, and __ as a slightly-less-soft mechanism. Both communicate the intention of the developer to say that these methods are implementation details. I thought I remembered an answer from DavidArno about this... Commented Oct 28, 2021 at 15:41
  • And how is that different from decorating with @classmethod? It seems like that would effectively convey the same thing. Commented Oct 28, 2021 at 15:47

1 Answer 1

3

There is no syntax in Python to define private methods. As such, there are two conventions that are considered Pythonic:

  • Prefixing your method name by a single underscore. This is not a special syntax, merely a way for a developer to communicate their intention that this method or attribute is to be considered an implementation details that clients should not rely upon. Clients may still choose to ignore this intention and call the method regardless.
  • Prefixing your method name by two underscores. As you hinted toward, this is a special syntax. Python performs name mangling and silently renames a function prefixed with two underscores. The intention is, as you said, to prevent name clashes in classes which subclass it.

I could not tell at a glance (I quickly looked at the Git history of the file) why the author of the code you linked to decided to use name mangling here. It is possible that an old version of the code did have subclasses, that the author wanted to prevent an issue in the future if the class were indeed subclassed, or simply that they used this syntax so that the method would be considered private by convention.

You mentioned the @classmethod and the @staticmethod decorators. These decorators have no effect on the visibility of the methods they decorate. They do not make such methods private, by convention or not. They simply denote methods that do not depend on or mutate the state of an instance of your class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.