using names before they're defined

Iain King iainking at gmail.com
Thu Jul 20 04:37:46 EDT 2006


davehowey at f2s.com wrote:
> Iain, thanks - very helpful.
>
> Really I'm trying to write a simulation program that goes through a
> number of objects that are linked to one another and does calculations
> at each object. The calculations might be backwards or fowards (i.e.
> starting at the supply or demand ends of the system and then working
> through the objects). And also, I might have multiple objects linked to
> a single object (upstream or downstream) - e.g. compressor -- multiple
> combusters - turbine
>
> I like your idea of using something like a setStreams method to
> establish the linking. The streams do reflect each other, although
> having many-to-one and vice versa will complicate that. I have not
> quite got my head around having multiple links. In C++ I would be
> thinking about something like a linked-list but I'm not sure that's the
> right approach here.
>
> Dave

You don't need linked-lists : python has a list type built in.
Example:

class Component():

    upstream = []
    downstream = []

    def addUpstream(self, c):
        self.upstream.append(c)
        if not self in c.downstream:
            c.addDownstream(self)

    def addDownstream(self, c):
        self.downstream.append(c)
        if not self in c.upstream:
            c.addUpstream(self)

    def remUpstream(self, c):
        c.downstream.remove(self)
        self.upstream.remove(c)

    def remDownstream(self, c):
        c.upstream.remove(self)
        self.downstream.remove(c)

    def cascadeDownTest(self):
        print self
        # this could run forever if you connect components in a circle:
        for c in self.downstream:
            c.cascadeDownTest() 

Iain




More information about the Python-list mailing list