Difference between _, __ and __xx__ in Python
许多人在学习python的时候并没有真正理解下划线的意义,下面给出详细的解释。
One underline in the beginning
Python doesn't have real private methods, so one underline in the beginning of a method or attribute means you shouldn't access this method, because it's not part of the API. It's very common when using properties:
Python没有像C++那样的私有域,使用一个下划线开头的方法或属性表示你无法通过对象名或类名访问该方法或属性,即该方法或属性是private的,对外部不可见。
对于properties同样适用。
class BaseForm(StrAndUnicode):
...
def _get_errors(self):
"Returns an ErrorDict for the data provided for the form"
if self._errors is None:
self.full_clean()
return self._errors
errors = property(_get_errors)
This snippet was taken from django source code (django/forms/forms.py). This means errors is a property, and it's part of the API,