以下结构:
app.py /package __init__.py foo.py bar.py“foo.py”和“bar.py”包含“Foo”和“Bar”类。 “Foo”类继承自“Bar”类。 我们在文件中有以下代码......
“app.py”:
from package import Foo print Foo()“的__init__.py”:
from foo import Foo from bar import Bar“foo.py”:
class Foo(Bar): pass“bar.py”:
class Bar: pass如果我从“Foo”创建一个实例,我就成了一个名称错误“name'Bar'未定义”。 我需要做些什么来使它工作? 我正在使用Python 2.6.6如果重要的话......
.oO(我是Python的新手)
Following structure:
app.py /package __init__.py foo.py bar.py"foo.py" and "bar.py" contains both classes "Foo" and "Bar". Class "Foo" inherits from class "Bar". We have following code in the files...
"app.py":
from package import Foo print Foo()"__init__.py":
from foo import Foo from bar import Bar"foo.py":
class Foo(Bar): pass"bar.py":
class Bar: passIf I create an instance from "Foo" I became an name error "name 'Bar' is not defined". What I have to do to make it work? I am using Python 2.6.6 if it matters...
.oO(I'm new to Python)
最满意答案
你需要这条线
from bar import Bar在文件foo.py (不仅仅是在__init.py__ )。
You need the line
from bar import Barin the file foo.py (not just in __init.py__).
更多推荐
发布评论