Python 类属性 下划线的意义是什么?

Python 类属性 下划线的意义是什么?

我发现 _A 是可以访问的但是 __B 是无法访问的会报错:Unexpect System Error:'A' object has no attribute ....

class A:

@property

def _A(self):

@property

def __B(self):

不是说下划线只是约定而已么,怎么其实还是有实际作用的


回答:

双下划线比单下划线多了 Name Mangling,这是个 Python 2.0 时引入的新特性。你可以直接理解成编译器会把属性名替换成别的。

但如果你知道编译器是怎样替换的(一般来说是 _类__属性,具体到题目中的例子就是 _A__B),你照样可以在外部访问到它。换而言之,它依然还是“公有”的,只是被编译器改了名字。

因此我们说 Python 并没有真正的属性可见性修饰符,下划线仅仅是约定俗成。


回答:

private variables

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

双下划线确实有“实际作用”。它会触发 name mangling ,实际存在的成员名字会被增加一个修饰。它在类外无法直接被使用,从而是“私有的”。


在类外使用修饰后的名字是可以访问的。

以上是 Python 类属性 下划线的意义是什么? 的全部内容, 来源链接: utcz.com/p/939048.html

回到顶部