_single_leading_underscore : weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.
single_trailing_underscore_ : used by convention to avoid conflicts with a Python keyword.
__double_leading_underscore : when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo).
__double_leading_and_trailing_underscore__ : "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__ , __import__ or __file__ . Never invent such names; only use them as documented.
单下划线只是一种命名约定,用于声明属性应被视为“半私有”(类似地,双下划线表示“私有”),但它没有语义上的区别:两个版本的代码的行为应该完全相同。根据PEP-8:
如果出于某种原因,您有一个以下划线为前缀的变量,并且可以公开访问该变量,那么最好在模块的__all__ list中包含该变量的名称。这是代码内文档的一种形式。在
相关问题 更多 >
编程相关推荐