关于python中导入模块

发布于 2024-11-19 08:40:01 字数 826 浏览 2 评论 0原文

我在同一个文件夹中有三个模块。

第一个模块 run.py 是主程序。

第二个模块名为 Shapes.py,包含一个名为“Shape”的类。

第三个模块名为 Circles.py,包含一个名为“Circle”的类,该类继承自 Shape。

代码编写如下

run.py

from shapes import Shape
from circles import Circle

a = Circle()
a.print_test()

Shapes.py

class Shape(object):

    def print_name(self):

        print "I am a generic shape"

Circles.py

class Circle(Shape):

    def print_name(self):

        print "I am a circle"

我希望能够运行该程序并让控制台说“我是一个圆”,但是在导入圆时它会抛出异常,说“形状不是定义”。

我总是可以告诉 Circles.py 导入 Shape 类,但这不是我想要的。如果它们不在同一个文件夹中怎么办?如果文件夹的层次结构很复杂怎么办?

感觉就像我不必要地导入形状模块两次,只是为了导入圆圈。

我能做些什么? (好吧,在这种情况下,run.py 可能甚至不需要导入 Shape,但是如果我有其他一些模块“三角形”、“六边形”和“五边形”,我不希望它们全部都必须导入形状)

编辑:我也可以将它们全部放在同一个模块中,因为它们是形状!但这种问题有时可能会出现。

I have three modules in the same folder.

The first module, run.py, is the main program.

The second module, called shapes.py, contains a class called "Shape"

The third module, called circles.py, that contains a class called "Circle" which inherits from Shape.

The code is written as follows

run.py

from shapes import Shape
from circles import Circle

a = Circle()
a.print_test()

shapes.py

class Shape(object):

    def print_name(self):

        print "I am a generic shape"

circles.py

class Circle(Shape):

    def print_name(self):

        print "I am a circle"

I want to be able to run the program and have the console say "I am a circle", but it throws an exception when importing circles saying that "Shape is not defined".

I can always tell circles.py to import the Shape class, but that's not what I want. What if they're not in the same folder? What if there's a complicated hierarchy of folders?

It feels like I'm importing the shapes module twice that way unnecessarily just so that I can import circles.

What can I do? (well, in this case, run.py probably doesn't even need to import Shape, but if I had some other modules "triangles", "hexagons", and "pentagons" I don't want them all to have to import Shape)

EDIT: I could also just put them all in the same module cause they're shapes! But this sort of problem might arise some time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风追烟花雨 2024-11-26 08:40:01

circles.py中导入shapes

from shapes import Shape

class Circle(Shape):
   ...

在Python中,每个模块都必须导入它需要的任何内容。它不能依赖任何其他模块来为其进行导入。

Import shapes from within circles.py:

from shapes import Shape

class Circle(Shape):
   ...

In Python, each module must import whatever it needs. It can't rely on any other module to do the importing for it.

简单 2024-11-26 08:40:01

您需要导入 python 模块中使用的所有类。在您的第一个模块(run.py)中,您没有使用Shape,仅使用Circle,因此您可以在那里省略它。 run.py 根本不需要知道Circle 是如何定义的。

然而,在circles.py中,您确实需要导入shapes,因为您的Circle类基于Shape > 类,因此它需要访问那里的定义。

from shapes import Shape

class Circle(Shape):
    def print_name(self):
         print "I am a circle"

我建议您使用 pyflakes 之类的工具来检查文件是否存在此类错误。您可以将这样的脚本与各种编辑器连接起来以自动运行,每当您保存 python 文件时都会给您即时反馈。

You need to import all classes you are using in a python module. In your first module (run.py) you are not using Shape, only Circle, so you can omit it there. run.py does not need to know how Circles are defined at all.

In circles.py however, you do need to import shapes, as you are basing your Circle class on the Shape class, so it needs to have access to the definition there.

from shapes import Shape

class Circle(Shape):
    def print_name(self):
         print "I am a circle"

I'd recommend you use a tool like pyflakes to check your files for errors like these. You can hook such a script up with various editors to be run automatically, giving you instant feedback whenever you save your python files.

昵称有卵用 2024-11-26 08:40:01

我认为你必须在圆类中初始化形状超类。您可以通过构造函数来完成此操作。因此,在您的circle.py 类中,您需要有如下内容:

class Circle(Shapes):
    def __init__(self):
        Shapes.__init__(self)
        # put the rest of your circle code here.

Magnus Lie Hetland 的书“Beginning Python: From Novice to Professional”很好地涵盖了这个领域。

I think you have to initialize the shapes superclass in the circle class. You can do this via constructors. So in your circle.py class you would need to have something like the following:

class Circle(Shapes):
    def __init__(self):
        Shapes.__init__(self)
        # put the rest of your circle code here.

Magnus Lie Hetland's book, "Beginning Python: From Novice to Professional" covers this area fairly well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文