Python中单下划线和双下划线

本文阅读 1 分钟
首页 › Python笔记 › 正文

__foo__:一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突,就是例如__init__(),__del__(),__call__()这些特殊方法

_foo:一种约定,用来指定变量私有.程序员用来指定私有变量的一种方式.不能用from module import * 导入,其他方面和公有一样访问;

__foo:这个有真正的意义:解析器用_classname__foo来代替这个名字,以区别和其他类相同的命名,它无法直接像公有成员一样随便访问,通过对象名._类名__xxx这样的方式可以访问.

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}
@staticmethod和@classmethod
« 上一篇 07-01
Python里的拷贝
下一篇 » 07-01

发表评论

惪特博客
  • 文章总数:
    5298 篇
  • 评论总数:
    53894 条
  • 标签总数:
    6607 个
  • 总浏览量:
    37577663 次
  • 最后更新:
    昨天 09:44

最多点赞

随便看看

标签TAG