[Python] When are python sunder names used?

Discussão em 'Python' iniciado por Stack, Julho 29, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página