覆盖孩子的父母进口?

时间:2015-02-10 04:40:42

标签: python inheritance

我一直在谷歌上搜索无济于事,也许我只是没有找到正确的东西......我所拥有的是基地的许多子类。该基础将具有从导入的模块获取类的实例的方法。我想要的是基类中的方法从子类中导入的模块中获取实例。

我有这样的结构:

root
|--Base
|----foo.py
|----bar.py
|--Child1
|----foo.py
|----bar.py
|--Child2
|----foo.py
|----bar.py

基本上有几个孩子都从Base继承。我想知道的是,是否可以使用从Base继承的方法,该方法使用子类对基类的导入:

# file Base/foo
from root.Base import bar

class Foo(object):
   def get_bar_instance(self):
       return bar.Bar(param)

# file Base/bar

class Bar(object):
   def do_some_stuff(self):
       print("Base")

然后在子类中:

# file Child1/foo
from root.Base import foo
from root.Child1 import bar

class Foo(foo.Foo):
""" Some stuff for subclass only"""

# file Child1/bar
from root.Base import bar

class Bar(bar.Bar):
   def do_some_stuff(self):
       print("Child1")

如果我这样做:

from root.Child1 import foo

temp_foo = foo.Foo()
temp_bar = temp_foo.get_bar_instance()
temp_bar.do_some_stuff()

我希望" Child1"打印。这可能吗?或者没有办法超越"父元素在子模块中的导入。这有点难以解释,因为它不仅仅是一种打印方法......我希望在父类中使用该方法,而不是在每个孩子中都使用该方法

0 个答案:

没有答案
相关问题