Import underscores

I am writing a simple Python library in which I have several "private" functions, starting with underscore:

def _a():
    pass

def _b():
    pass

def public_interface_call():
    _a()
    _b()

That way, library users can simply do it from MyLib.Module import *, and their namespace will not be cluttered with implementation details.

However, I also write unit tests in which I would like to test these functions separately and just importing really all the characters from my module would be very convenient. I am currently studying from Mylib.Module import _a _b public_interface_call, but I am wondering if there is a better / faster / cleaner way to achieve what I want?

+6
source share
3 answers

, - , , ( Underyx),

import MyLib.Module

MyLib.Module._a()
MyLib.Module._b()

( ):

import MyLib.Module as mm

mm._a()
mm._b()
+6

docs,

, :
from fibo import *
...
, , (_).

, .

+1

If you are not doing a serious program, you can try this

import somemodule
vars = {name: getattr(somemodule, name) for name in
            dir(somemodule) if not name.startswith('__')}
globals().update(vars)
del vars, somemodule
0
source

Source: https://habr.com/ru/post/1598717/


All Articles