关于Python:underscore underscore与双变量和方法。

Underscore vs Double underscore with variables and methods

本问题已经有最佳答案,请猛点这里访问。

有人很好地向我解释了uu method()mangles,但没有进一步打扰他,因为有很多其他人需要帮助,我想知道是否有人可以进一步阐述这些差异。

例如,我不需要犯规,但是否保持私人,所以有些人不能做例证。或者它只是通过使另一个变量具有唯一性来防止它覆盖它?我不需要"隐藏"我的内部方法,但是由于它们是特定的使用方法,我不希望它们在类外被使用。


来自PEP 8:

  • _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 Python keyword, e.g.

    Tkinter.Toplevel(master, class_='ClassName')

  • __double_leading_underscore: when naming a class attribute, invokes name
    mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).

  • __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.

此外,从大卫·古德的《像Python一样的密码》来看:

Attributes: interface, _internal, __private

But try to avoid the __private form. I never use it. Trust me. If you
use it, you WILL regret it later.

Explanation:

People coming from a C++/Java background are especially prone to
overusing/misusing this"feature". But __private names don't work the
same way as in Java or C++. They just trigger a name mangling whose
purpose is to prevent accidental namespace collisions in subclasses:
MyClass.__private just becomes MyClass._MyClass__private. (Note that
even this breaks down for subclasses with the same name as the
superclass, e.g. subclasses in different modules.) It is possible to
access __private names from outside their class, just inconvenient and
fragile (it adds a dependency on the exact name of the superclass).

The problem is that the author of a class may legitimately think"this
attribute/method name should be private, only accessible from within
this class definition" and use the __private convention. But later on,
a user of that class may make a subclass that legitimately needs
access to that name. So either the superclass has to be modified
(which may be difficult or impossible), or the subclass code has to
use manually mangled names (which is ugly and fragile at best).

There's a concept in Python:"we're all consenting adults here". If
you use the __private form, who are you protecting the attribute from?
It's the responsibility of subclasses to use attributes from
superclasses properly, and it's the responsibility of superclasses to
document their attributes properly.

It's better to use the single-leading-underscore convention,
_internal."This isn't name mangled at all; it just indicates to
others to"be careful with this, it's an internal implementation
detail; don't touch it if you don't fully understand it". It's only a
convention though.


一个前导下划线只是一个约定,意思是"你可能不应该使用这个。"它不会阻止某人使用该属性。

双前导下划线实际上会更改属性的名称,以便继承层次结构中的两个类可以使用相同的属性名称,并且不会发生冲突。


python中没有访问控制。您可以访问一个类的所有属性,其中包括损坏的名称(如_class__variable)。专注于您的代码和API,而不是试图保护开发人员不受其影响。