天天看点

The differences of _single_starting_underscore, __double_starting_underscores, __double_underscores__

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.