Python中除去常见的普通属性外还存在私有属性与受保护的属性.
这两种属性往往是设计者不希望他人看到的属性,在类设计过程中会采用下划线来标识.
私有属性双下划线开头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class vector: def __init__(self , x, y ,z): self.__x = x self.__y = y self.z = z def __repr__(self): class_name = type(self).__name__ return '{}({!r}, {!r} , {!r})'.format(class_name, *self)
def __str__(self): return str(tuple(self)) def __iter__(self): return (i for i in (self.__x, self._vector__y , self.z)) vec = vector(3,4, 5) print(vec)
print(vec.__dict__)
print(vec._vector__x)
print(vec.__x)
|
大多数Python程序员反对使用双下划线进行命名操作,于是提出使用单下划线前缀编写受保护的属性,约定不对单下划线进行访问.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class vector: def __init__(self , x, y ,z): self._x = x self.__y = y self.z = z def __repr__(self): class_name = type(self).__name__ return '{}({!r}, {!r} , {!r})'.format(class_name, *self)
def __str__(self): return str(tuple(self)) def __iter__(self): return (i for i in (self._x, self._vector__y , self.z)) vec = vector(3,4, 5) print(vec) print(vec.__dict__) print(vec._x)
|
上面的代码实例可以看到单下划线的属性,Python解释器并没有对其进行改名操作,就当作普通属性,这就是一种约定而已.