Search code examples
pythonnamingpep8

Can we name python functions (not methods!) with leading underscores?


According to PEP8 we should "use one leading underscore only for non-public methods" [of classes].

Can we use the same idea to distinguish functions (=not belonging to any class) inside modules (=files) between "public" functions called from outside the module and "non-public" functions called only from inside the module? Or is this not recommended? Not sure how strictly to interpret the guideline above.

//EDIT: Side question: If it is prohibited, is there another helpful syntax which can be used for that purpose?


Solution

  • Credit goes to @CoryKramer for pointing out the relevant section

    In addition, the following special forms using leading or trailing underscores are recognized [...]:

    • _single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose names start with an underscore.