using names before they're defined

Nick Vatamaniuc vatamane at gmail.com
Wed Jul 19 12:35:26 EDT 2006


Your can of course initialize the components first:

compr=Compressor(...),
comb=Combuster(...),
sup=Supply(...) ,
tur=Turbine(...).

Then do:

compr.up, compr.down =sup, comb
comb.up,  comb.down   =compr, tur

Even if you need to do something during attachment of components it is
more Pythonic to use properties. So you will write a method in your
class name something like _set_up(self,upstream_obj) an  _get_up(self).
 And then at the end of your class put up=property(_get_up, _set_up).
You can still use the compr.up=... format.

Also, you might want to re-think your OO design. It seem that all of
your components do a lot of things in common already. For one they all
are connected to other components like themselves, they also propably
will have method to do some computing, perhaps send or receive stuff
from other components, or they all will implement somekind of an event
model. In that case you could create a generic component class and
sublass the specific implementations from it. For example:
class Component(object):
    send(...)
    recv(...)
    up
    down
    print_data(...)
    ...

Then do:
 class Turbine(Component):
        method_specific_to_turbine(...)
        send(...) #override some methods
        ...
and so on. Of course I am not familiar with your problem in depth all
this might not work for you, just use common sense.

Hope this helps,
Nick Vatamaniuc

davehowey 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




More information about the Python-list mailing list