Python: 'Private' module in a package

前端 未结 5 782
小蘑菇
小蘑菇 2020-12-05 06:23

I have a package mypack with modules mod_a and mod_b in it. I intend the the package itself and mod_a to be imported free

5条回答
  •  悲&欢浪女
    2020-12-05 06:46

    While there are not explicit private keywords there is a convention to have put private functions start with a single underscore but a double leading underscore will make it so others cannot easily call the function from outside the module. See the following from PEP 8

    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
      import *" does not import objects whose name starts with an underscore.
    
    - single_trailing_underscore_: used by convention to avoid conflicts with
      Python keyword, e.g.
    
      Tkinter.Toplevel(master, class_='ClassName')
    
    - __double_leading_underscore: when naming a class attribute, invokes name
      mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
    
    - __double_leading_and_trailing_underscore__: "magic" objects or
      attributes that live in user-controlled namespaces.  E.g. __init__,
      __import__ or __file__.  Never invent such names; only use them
      as documented.
    

    To make an entire module private, don't include it __init__.py file.

提交回复
热议问题