Python 使模块类的导入全局工作

Python 使模块类的导入全局工作,python,import,module,Python,Import,Module,我有一个python模块,它包含几个(继承的)类。我们称之为foomod: class Base(object): def interface_func(self): pass class Child1(Base): def interface_func(self): #do something class Child2(Base): def interface_func(self): #do something els

我有一个python模块,它包含几个(继承的)类。我们称之为foomod:

class Base(object):
    def interface_func(self):
        pass

class Child1(Base):
    def interface_func(self):
        #do something

class Child2(Base):
    def interface_func(self):
        #do something else
我在另一个脚本中使用了它,如下所示:

from foomod import Child1 as Base

def some_function(*params):
    b = Base()
    result = b.interface_func()
    #....
wrapper_func(child):
   if child == 1:
      from foomod import Child1 as Base
    #....
    while True:
       yield Base()

child = wrapper_func(args.child)

#usage
def some_function(*params):
    b = next(child)
然后,我选择通过问题开始的cmd参数使其可设置:

if __name__ == "__main__":
    args = parse_args() #'ArgumentParser args'
    if args.child == 1:
       from foomod import Child1 as Base
    elif args.child == 2:
       from foomod import Child2 as Base
    #....
这给了我一个执行错误:

。。。在某些功能行XXX中

未定义全局名称“Base”

我知道我可以这样做:

from foomod import Child1 as Base

def some_function(*params):
    b = Base()
    result = b.interface_func()
    #....
wrapper_func(child):
   if child == 1:
      from foomod import Child1 as Base
    #....
    while True:
       yield Base()

child = wrapper_func(args.child)

#usage
def some_function(*params):
    b = next(child)
但这感觉很难看。。。
有更好的解决方案吗?

如果uuuu name uuuuu==“uuuu main uuuu”与
某些函数
在同一个文件中,那么
是吗?是的!我只是想让它在这个文件中可以访问为
Base
,使用Python2.7btw,如果导入是在模块级别,那么您描述的应该可以工作。你能提供一个独立的例子来演示这个问题吗?不,我不能,因为我刚刚玩了代码,在我的参数中有一个错误。。。没有其他结局。。。所以它只是没有定义