循环继承 [英] Circular Inheritance

查看:58
本文介绍了循环继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个关于循环继承的问题


我有3个文件


1)A.py,有模块A和其他一些模块

2)B.py有模块B和其他一些模块

3)C.py有模块C和其他一些模块


现在我想在B.py中导入模块C


但是C需要A.py

和A需要B.py


所以


B需要C

C需要A

A要求B


当我尝试使用


来自......导入时... ..

它告诉我你不能导入它。


那么对此有什么建议吗?


谢谢你好

Jinal

Hi All,

I have one question regarding circular inheritance

I have 3 files

1) A.py , having module A and some other modules
2) B.py having module B and some other modules
3) C.py having module C and some other modules

Now I want to import Module C in B.py

but C requires A.py
and A requires B.py

so

B requires C
C requires A
A requires B

and when I try to do this using

from ...import....
it tells me that you cannot import this.

So any suggestions on this?

thank you
Jinal

推荐答案

2003年7月1日星期二15:36:11 -0700,jinal jhaveri写道:
On Tue, 01 Jul 2003 15:36:11 -0700, jinal jhaveri wrote:
B需要C
C需要A
A需要B

当我尝试使用 from ... import ....
它告诉我你不能导入它。
B requires C
C requires A
A requires B

and when I try to do this using
from ...import....
it tells me that you cannot import this.




不要那么做。


重新设计模块,使继承成为层次结构。一个创可贴的解决方案可能是创建一个新的课程,在另外两个人之间进行调解,打破这个圈子。更好的是弄清楚为什么课程需要相互了解,并用更小的,更简单的b $ b b更简单的界面重新设计它们。

即使有办法进行循环继承,理解这也是一个噩梦,所以需要重新设计。


-

\我开始认为生活只是一个长的小野洋子|

` \ album;没有押韵或理由,只有很多不连贯的尖叫声和|

_o__)然后它就结束了。 - Ian Wolff |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B



Don''t Do That Then.

Re-design the modules so that inheritance is a hierarchy. A band-aid
solution may be to create a new class that mediates between two others,
breaking the circle. Much better is to figure out why the classes need
to know so much about each other, and redesign them with smaller,
simpler interfaces.

Even if there were a way to do circular inheritance, it would be a
nightmare to understand, so needs to be redesigned anyway.

--
\ "I''m beginning to think that life is just one long Yoko Ono |
`\ album; no rhyme or reason, just a lot of incoherent shrieks and |
_o__) then it''s over." -- Ian Wolff |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




" jinal jhaveri" < JH ***** @ usc.edu>在留言中写道

新闻:ma ********************************** @ python .o rg ...

"jinal jhaveri" <jh*****@usc.edu> wrote in message
news:ma**********************************@python.o rg...
大家好,

我有一个关于循环继承的问题

我有3个文件

1)A.py,有模块A和其他一些模块
2)B.py有模块B和其他一些模块
3)C.py有模块C和其他一些模块
现在我要在B.py中导入模块C

但是C需要A.py
而A需要B.py

所以

B需要C
C需要A
A需要B

当我尝试使用

时。 ..import ....
它告诉我你不能导入它。

那么对此有何建议?


如果你记得Python *执行*
$ b $你已经碰到了其中一件只有

可以理解的事情b导入过程中的模块。


当你说import时某事,Python暂停

导入第一个模块,然后开始导入

秒。


然后如果第二个尝试导入第一个,它看到

它在模块表中,并试图找到你要求的b $ b b;但它已经不完整,因为它已经停止在

第一次进口了。声明所以它可能找不到

无论它在寻找什么。


最好的解决方法是取消通告

依赖。结果更清晰,更容易维持。


否则,你必须非常明确

去哪里每个模块,以便当其他

执行时,实际上已经加载了任何想要的资源。特别是,来自foo import *只是

无法使用循环导入。如果它不在

不完整的模块中,它就不会被导入。


John Roth

<谢谢你
Jinal
Hi All,

I have one question regarding circular inheritance

I have 3 files

1) A.py , having module A and some other modules
2) B.py having module B and some other modules
3) C.py having module C and some other modules

Now I want to import Module C in B.py

but C requires A.py
and A requires B.py

so

B requires C
C requires A
A requires B

and when I try to do this using

from ...import....
it tells me that you cannot import this.

So any suggestions on this?
You''ve run into one one of those things that is only
understandable if you remember that Python *executes*
the module during the import process.

When you say "import" something, Python suspends
importing the first module, and begins importing the
second.

Then if the second one tries to import the first, it sees
that it''s in the module table, and tries to find what you
requested; but it''s incomplete since it''s stalled at the
first "import" statement so it probably won''t find
whatever it was looking for.

The best way around this is to eliminate the circular
dependency. The result is clearer and easier to
maintain.

Otherwise, you have to be very specific about what
goes where in each module so that any resources that
one wants have actually been loaded when the other
executes. In particular, "from foo import *" simply
doesn''t work with a circular import. If it''s not in
the incomplete module, it won''t get imported.

John Roth


thank you
Jinal



在星期二,2003-07-01 19:00,Ben Finney写道:
On Tue, 2003-07-01 at 19:00, Ben Finney wrote:
重新设计模块,使继承成为层次结构。创可贴解决方案可能是创建一个新的课程,在另外两个人之间进行调解,打破这个圈子。更好的是弄清楚为什么这些类需要彼此了解很多,并用更小,更简单的接口重新设计它们。


在两个或多个b
之间有循环依赖关系似乎很合理。最常见的情况是一个类引用了

other,另一个类有后引用。很常见,并且它不会暗示这些课程对彼此了解太多。


如果这些课程一起出现,你可能遇到的问题会更少在一个

单个模块中,但模块边界只是帮助

程序员组织代码,它们几乎没有正式意义。

即使有一种循环继承的方法,这将是一个理解的噩梦,所以无论如何都需要重新设计。
Re-design the modules so that inheritance is a hierarchy. A band-aid
solution may be to create a new class that mediates between two others,
breaking the circle. Much better is to figure out why the classes need
to know so much about each other, and redesign them with smaller,
simpler interfaces.
It seems quite reasonable to have circular dependencies between two or
more classes. The most common case being that one class references the
other, and the other class has back references. Very common, and it
doesn''t imply that the classes know too much about each other.

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.
Even if there were a way to do circular inheritance, it would be a
nightmare to understand, so needs to be redesigned anyway.




他可能只是意味着循环依赖,因为显然继承

没有意义。


Ian



He probably just meant circular dependency, since obviously inheritance
doesn''t make sense.

Ian


这篇关于循环继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆