using names before they're defined
Iain King
iainking at gmail.com
Wed Jul 19 11:40:49 EDT 2006
daveho... at f2s.com wrote:
> 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
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
More information about the Python-list
mailing list