1. Define a method or parameter with an underscore as the starting
If a method starts with an underscore, it only can be called by this class itself or subclass. Of course, it can not be imported by using "from module import *".
2. Define a method or parameter with double underscores as the starting not an ending
It looks like a private method or parameter. It can't be called outside the class, including the subclass. Of course, it can't be overridden by subclass.
3. Define a method or parameter with double underscores as the starting and ending
FYI: http://docs.python.org/reference/datamodel.html#specialnames
Summary:
Named special method name.
You are not supposed to call these methods or parameters directly although they are not declared as private. Because they are called by python. Such as the method __getitem__(), __init__() ---called when the instance is created, __getattr__(), etc...
Besides, you are not suggested to define a common method or parameter with the underscores on the starting or ending position.
本文详细介绍了Python中定义方法或参数的不同方式,包括单下划线开头的方法仅能被类本身及其子类调用;双下划线开头的方法在类外部无法直接访问,也无法在子类中覆盖;双下划线开头和结尾的方法为特殊方法,由Python内部使用。


被折叠的 条评论
为什么被折叠?



