从包中继承加载类(Loading classes with inheritance from package)

编程入门 行业动态 更新时间:2024-10-27 21:13:27
包中继承加载类(Loading classes with inheritance from package)

以下结构:

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: pass

If 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 Bar

in the file foo.py (not just in __init.py__).

更多推荐

本文发布于:2023-08-01 04:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352777.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:包中   加载   Loading   package   inheritance

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!