Accesing private module variable from class

后端 未结 1 1212
[愿得一人]
[愿得一人] 2021-01-03 12:44

I\'m trying understand python scope rules. To do this I try access \"very private\" variable from class in same module

bar = \"bar\"
_bar = \"underscore\"
_         


        
相关标签:
1条回答
  • 2021-01-03 13:32

    Within a class definition, all names starting with double underscores are mangled; rewritten to include the class name as a prefix.

    This is a feature to support marking names as 'private' within the class and protect it against being overwritten by subclasses. See the identifiers documentation:

    Private name mangling: When an identifier that textually occurs in a class definition begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the identifier __spam occurring in a class named Ham will be transformed to _Ham__spam. This transformation is independent of the syntactical context in which the identifier is used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may happen. If the class name consists only of underscores, no transformation is done.

    Best not use double-underscore prefixes on module globals; there is no need to do so, a single underscore is enough to communicate that the value is internal to the module.

    If you are stuck with such a value, create an alias that isn't mangled, or use globals()[name].

    0 讨论(0)
提交回复
热议问题