python输出下划线_python中的下划线

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,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值