gpt4 book ai didi

python - 带变量和方法的下划线与双下划线

转载 作者:IT老高 更新时间:2023-10-28 12:34:05 24 4
gpt4 key购买 nike

有人很好地向我解释了 __method() 损坏了,但由于还有很多其他人需要帮助,所以没有进一步打扰他,我想知道是否有人可以进一步详细说明这些差异。

例如,我不需要修改,但 _ 是否保持私有(private),因此有人不能执行 instance._method()?或者它只是通过使其唯一来防止它覆盖另一个变量?我不需要“隐藏”我的内部方法,但由于它们是特定于使用的,我不希望它们在类之外使用。

最佳答案

来自 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.

另外,来自 David Goodger 的 Code Like a Pythonista :

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 - 带变量和方法的下划线与双下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6930144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com