[Tutor] Why begin a function name with an underscore

罗健忠 newkedison at gmail.com
Tue Aug 28 02:34:26 CEST 2012


>
> It's better to use the single-leading-underscore convention, _internal.
> This isn't name mangled at all; it just indicates to others to "be careful
> with this, it's an internal implementation detail; don't touch it if you
> don't *fully* understand it". It's only a convention though.


you can read some other info for naming in python here:
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming

On 28 August 2012 08:26, Richard D. Moores <rdmoores at gmail.com> wrote:

> I've been going through Steven D'Aprano's pyprimes
> (<http://pypi.python.org/pypi/pyprimes/0.1.1a>) and have many
> questions. One is why begin a function name with an underscore. He has
> several functions that do this. Two are:
>
> def _validate_int(obj):
>     """Raise an exception if obj is not an integer."""
>     m = int(obj + 0)  # May raise TypeError.
>     if obj != m:
>         raise ValueError('expected an integer but got %r' % obj)
>
>
> def _validate_num(obj):
>     """Raise an exception if obj is not a finite real number."""
>     m = obj + 0  # May raise TypeError.
>     if not isfinite(m):
>         raise ValueError('expected a finite real number but got %r' % obj)
>
> I'm stealing these for their usefulness. But why the underscores?
>
> I think I've also seen elsewhere variable names that also begin with
> an underscore, e.g., _a .
>
> Dick Moores
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
上海中核维思仪器仪表有限公司
罗健忠
电话:021-57850218-225
手机:13764515733
邮箱/GTalk:newkedison at gmail.com <http://www.google.com>

IMPORTANT NOTE: This e-mail and any attachment are confidential and may
contain trade secrets and may also be legally privileged or otherwise
protected from disclosure. If you have received it in error, you are
herewith informed about its status. Please notify us immediately by reply
e-mail and then delete this e-mail and any attachment from your system. You
are prohibited from making use of or copying this e-mail or any attachment
or disclosing the contents to any other person.

重要提示:此邮件及附件具保密性质,包含商业秘密、受法律保护不得泄露。如果您意外收到此邮件,特此提醒您此邮件的机密性,请立即通知我们并从您的系统中删除此邮件及附件。如果您不是此邮件应当的收件人,请注意不可对此邮件及其附件进行利用、复制或向他人透露其内容。
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120828/f22bff12/attachment-0001.html>


More information about the Tutor mailing list