私人名称修改有什么好处?

发布于 2024-07-27 19:51:09 字数 299 浏览 9 评论 0原文

Python 为类方法和属性提供私有名称修改

是否有任何具体情况需要此功能,或者它只是 Java 和 C++ 的延续?

请描述一个应该使用Python名称修饰的用例(如果有的话)?

另外,我对作者只是试图防止意外的外部属性访问的情况不感兴趣。 我认为这个用例与 Python 编程模型不一致。

Python provides private name mangling for class methods and attributes.

Are there any concrete cases where this feature is required, or is it just a carry over from Java and C++?

Please describe a use case where Python name mangling should be used, if any?

Also, I'm not interested in the case where the author is merely trying to prevent accidental external attribute access. I believe this use case is not aligned with the Python programming model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

廻憶裏菂餘溫 2024-08-03 19:51:09

部分原因是为了防止意外的内部属性访问。 这是一个示例:

在您的代码中,这是一个库:

class YourClass:
    def __init__(self):
        self.__thing = 1           # Your private member, not part of your API

在我的代码中,我从您的库类继承:

class MyClass(YourClass):
    def __init__(self):
        # ...
        self.__thing = "My thing"  # My private member; the name is a coincidence

如果没有私有名称修饰,我意外重用您的名称会破坏您的库。

It's partly to prevent accidental internal attribute access. Here's an example:

In your code, which is a library:

class YourClass:
    def __init__(self):
        self.__thing = 1           # Your private member, not part of your API

In my code, in which I'm inheriting from your library class:

class MyClass(YourClass):
    def __init__(self):
        # ...
        self.__thing = "My thing"  # My private member; the name is a coincidence

Without private name mangling, my accidental reuse of your name would break your library.

溺孤伤于心 2024-08-03 19:51:09

来自 PEP 8

如果您的类打算进行子类化,并且您有不希望子类使用的属性,请考虑使用双前导下划线命名它们,并且不使用尾随下划线。 这会调用 Python 的名称修饰算法,其中类的名称被修饰为属性名称。 这有助于避免子类无意中包含同名属性时发生属性名称冲突。

(已添加强调)

From PEP 8:

If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores. This invokes Python's name mangling algorithm, where the name of the class is mangled into the attribute name. This helps avoid attribute name collisions should subclasses inadvertently contain attributes with the same name.

(Emphasis added)

坦然微笑 2024-08-03 19:51:09

以前的所有答案都是正确的,但这里有另一个原因和例子。 Python 中需要名称重整,以避免因覆盖属性而导致的问题。 换句话说,为了覆盖,Python 解释器必须能够为子方法与父方法构建不同的 id,并使用 __(双下划线)使 python 能够做到这一点。 在下面的示例中,如果没有 __help,此代码将无法工作。

class Parent:
    def __init__(self):
       self.__help("will take child to school")
    def help(self, activities):
        print("parent",activities)

    __help = help   # private copy of original help() method

class Child(Parent):
    def help(self, activities, days):   # notice this has 3 arguments and overrides the Parent.help()
        self.activities = activities
        self.days = days
        print ("child will do",self.activities, self.days)


# the goal was to extend and override the Parent class to list the child activities too
print ("list parent & child responsibilities")
c = Child()
c.help("laundry","Saturdays")

All previous answers are correct but here is another reason with an example. Name Mangling is needed in python because to avoid problems that could be caused by overriding attributes. In other words, in order to override, the Python interpreter has to be able to build distinct id for child method versus parent method and using __ (double underscore) enable python to do this. In below example, without __help this code would not work.

class Parent:
    def __init__(self):
       self.__help("will take child to school")
    def help(self, activities):
        print("parent",activities)

    __help = help   # private copy of original help() method

class Child(Parent):
    def help(self, activities, days):   # notice this has 3 arguments and overrides the Parent.help()
        self.activities = activities
        self.days = days
        print ("child will do",self.activities, self.days)


# the goal was to extend and override the Parent class to list the child activities too
print ("list parent & child responsibilities")
c = Child()
c.help("laundry","Saturdays")
擦肩而过的背影 2024-08-03 19:51:09

名称修改是为了防止意外的外部属性访问。 大多数情况下,它是为了确保不存在名称冲突。

The name mangling is there to prevent accidental external attribute access. Mostly, it's there to make sure that there are no name clashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文