简体   繁体   中英

Should I start internal methods with _?

If I have an object that has several internal functions that are not exactly meant for outside calls. Should I always start them with _ or __ ?

It is indicated by PEP-8 that you should use a single underscore to indicate that these should be of internal use:

In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

  • _single_leading_underscore : weak "internal use" indicator. Eg from M import * does not import objects whose name starts with an underscore.

Double leading underscores can also be used but, those are subject to name mangling:

  • __double_leading_underscore : when naming a class attribute, invokes name mangling (inside class FooBar , __boo becomes _FooBar__boo ).

so be aware of that fact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM