从 89% 高危红线到 13% 安全区:3 款降 AI 工具极限救场测评
2026/10/26 1:42:32
许多C++和Java程序员初涉Python时,会惊讶于Python没有private关键字。Python中最接近的概念是“名称修饰”(name mangling)。每当属性以__为前缀时,解释器会动态重命名该属性。示例如下:
>>> class MyClass(object): ... __secret_value = 1 ... >>> instance_of = MyClass() >>> instance_of.__secret_value Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'MyClass' object has no attribute '__secret_value' >>> dir(MyClass) ['_MyClass__secret_value', '__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'