Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are python sunder names used?

In Python, there are conventions for single leading underscore, double leading underscore, double leading + double trailing underscore, and single trailing underscore. Many of those are outlined in the answers at What is the meaning of a single- and a double-underscore before an object name?.

But what is the meaning or convention for single leading + single trailing underscore? I've first seen their use in the enum module:

8.13.15.3.2. Supported _sunder_ names

  • _name_ – name of the member
  • _value_ – value of the member; can be set / modified in new
  • _missing_ – a lookup function used when a value is not found; may be overridden
  • _ignore_ – a list of names, either as a list() or a str(), that will not be transformed into members, and will be removed from the final class
  • _order_ – used in Python 2/3 code to ensure member order is consistent (class attribute, removed during class creation)
  • _generate_next_value_ – used by the Functional API and by auto to get an appropriate value for an enum member; may be overridden

I have not seen such single-leading-single-trailing underscore sunder names before. Are they treated in any special way, or otherwise have an implied meaning that is distinct from any of the other underscore-related naming conventions? How are they different from having no underscores at all?

like image 208
gerrit Avatar asked Aug 24 '18 14:08

gerrit


People also ask

Why use underscores in Python?

It's a hint to the programmer—and it means what the Python community agrees it should mean, but it does not affect the behavior of your programs. The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use.

What do 2 underscores mean in Python?

Double underscores are used for fully private variables. According to Python documentation − If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores.

Should enums be capitalized Python?

Notice that enumerations are not normal Python classes, even though they are defined in a similar way. As enumerations are meant to represent constant values, it is advisable to uppercase the enum member names.


1 Answers

They are not treated in any special way. They are being used by the enum module so as to

  • Not to be accidentally overridden

e.g.

class Status(Enum):
    alive = auto()
    dead = auto()
    missing = auto()

You can see that Status.missing and Status._missing_ are different objects. If the Enum method _missing_ were named missing, we would have overridden it.

  • not appear as private. The name _value in python is considered private. To express that these are not private (which again, a user might want an enum value to be private), they are instead given sunder names

  • other alternatives such as __double_leading_underscore and __dunder__ also have special meanings in python as you stated above. _sunder_ methods in Enum act something like the __dunder__ protocols of pure python, but aren't reserved by the language.

Basically, it's an option to avoid attribute name conflicts without giving the wrong impression.

like image 62
FHTMitchell Avatar answered Oct 24 '22 04:10

FHTMitchell