仅导入大型图书馆的某些部分,但为了方便起见隐藏了图书馆的结构

时间:2018-11-21 08:47:22

标签: python python-import

请查看示例。有可能做到这一点吗?

# 1. importing from a large package lpack
#    only those parts that are going to be used
from lpack import timers    # defines SomeTimer and other Timers
from lpack import triggers  # defines RegularTrigger and others Triggers
# not importing many many other lpack modules

# 2. in the *same .py file* not having to care
#    about the internal organization of the lpack
mytimer = lpack.SomeTimer()        # i.e. not timers.SomeTimer()
mytrigger = lpack.RegularTrigger()

我没有解决办法。我的想法类似于lpack = timers + triggers(当然不是字面上的意思)。自动化方式(某种所需的导入副作用)将是最好的。

1 个答案:

答案 0 :(得分:0)

如何?

from lpack.timers import *   
from lpack.triggers import *

mytimer = SomeTimer()
mytrigger = RegularTrigger()

缺点是,如果两个包都包含相同名称的方法,则其中一个将被覆盖(我希望第二个导入将覆盖第一个)。您还应该注意不要使用具有相同名称的本地方法,因为这会导致冲突。