本文介绍了在定义之前使用名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题。我正在编写一个模拟程序,其中包含许多

机械组件,表示为对象。当我创建实例

的对象时,我需要引用(链接)每个对象

上游和下游,即


供应=供应()

压缩机=压缩机(下游=燃烧室,上游=供应)

燃烧器=燃烧器(下游=涡轮,上游=压缩机)




这个问题是我之前引用''燃烧器''

创建。如果我交换第2行和第3行我得到同样的问题

(压缩机在创建之前被引用)。

aargh !!!有关解决这个问题的任何想法吗?


Dave

I have a problem. I''m writing a simulation program with a number of
mechanical components represented as objects. When I create instances
of objects, I need to reference (link) each object to the objects
upstream and downstream of it, i.e.

supply = supply()
compressor = compressor(downstream=combustor, upstream=supply)
combuster = combuster(downstream=turbine, upstream=compressor)
etc.

the problem with this is that I reference ''combustor'' before is it
created. If I swap the 2nd and 3rd lines I get the same problem
(compressor is referenced before creation).
aargh!!! any ideas on getting around this?

Dave

推荐答案



在您的代码顶部,您可以放置​​:


供应=无

压缩机=无

燃烧器=无

涡轮机=无


但是,安排你的代码可能会更好:

supply = Supply()

compressor = Compressor()

combuster = Combuster()

turbine = Turbine()

compressor.setStreams(down = combuster,up = supply)

combuster .setStreams(down = turb,up = compressor)


流是否相互反映?也就是说,如果supply.down是压缩机的b $ b $,那么压缩机供应量是多少?在这种情况下你可能想要

做类似的事情:


class Component():


upstream =没有

下游=无


def setUpstream(self,c):

self.upstream = c

if c.downstream!= self:

c.setDownstream(self)


def setDownstream(self,c):

self.downstream = c

如果c.upstream!= self:

c.setUpstream(self)


类供应(组件):

通过





Iain

At the top of your code you could put:

supply = None
compressor = None
combuster = None
turbine = None

It might be better, though, to arrange your code like:
supply = Supply()
compressor = Compressor()
combuster = Combuster()
turbine = Turbine()
compressor.setStreams(down=combuster, up=supply)
combuster.setStreams(down=turbine, up=compressor)

Do the streams reflect each other? That is, if supply.down is
compressor, is compressor.up supply? In that case you probably want to
do something like:

class Component():

upstream = None
downstream = None

def setUpstream(self, c):
self.upstream = c
if c.downstream != self:
c.setDownstream(self)

def setDownstream(self, c):
self.downstream = c
if c.upstream != self:
c.setUpstream(self)

class Supply(Component):
pass

etc.

Iain




是的。你正在构建一个通用的数据结构,所以你不应该试图将各个对象存储在这样的变量中。你需要一个适合你问题的

数据结构。


例如,你可以考虑将它们存储在一个列表中,这样你就有了


components = [supply(),compressor(),combuster()]

然后components [n]是组件的上游[n- 1]和下游

组件[n + 1]。


简而言之,您对数据表示的思考可能需要成为一个

稍微复杂一点。


问候

Steve

-

Steve Holden + 44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd

Skype:holdenweb

最近的Ramblings

Yes. You are building a generic data structure, so you shouldn''t really
be trying to store individual objects in variables like that. You need a
data structure that''s appropriate to your problem.

For example, you could consider storing them in a list, so you have

components = [supply(), compressor(), combuster()]

Then components[n] is upstream of components[n-1] and downstream of
components[n+1].

In short, your thinking about data representation might need to become a
little more sophisticated.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden



这篇关于在定义之前使用名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:03