Hide external modules when importing a module (e.g. regarding code-completion)

后端 未结 4 422
慢半拍i
慢半拍i 2020-12-09 09:11

I have several modules in one package (a kind of a toolkit), which I use in my projects. The structure looks like this:

the_toolkit:
    __init__.py
    basi         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 09:30

    An alternative solution I came up with is to implement a short API class in the __init__ file which can make use of the __all__ variable (which may already be defined for import * purposes). Something like this in __init__.py:

    import matrix_kit
    
    class _matrix_kit_API:
        def __init__(self):
            for func in matrix_kit.__all__:
                setattr(self, func, eval(f"matrix_kit.{func}"))
    
    matrix_kit = _matrix_kit_API()
    

提交回复
热议问题