using names before they're defined

Steve Holden steve at holdenweb.com
Thu Jul 20 03:29:28 EDT 2006


jordan.nick at gmail.com wrote:
> Steve Holden wrote:
> 
>>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?
>>>
>>
>>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].
> 
> 
> Unfortunately, if he wanted to make the topology more complicated, for
> instance having two components downstream, it would be much more
> cumbersome to inherit the list object and implement this.
> 
I quite agree. That was why I began with "for example".
> 
>>In short, your thinking about data representation might need to become a
>>little more sophisticated.
> 
> 
> That sounds a little arrogant. sorry!
> 
Hmm, maybe it does, in which case I apologise retrospectively. But all 
the more appropriate suggestions have definitely been pointing that way.

Circuit design is a complex problem. I was merely trying to indicate 
that the naive approach was unlikely to succeed. Sure enough, a few 
posts later the OP admitted that there needed to be sub-chains for 
sub-circuits, and so on.

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




More information about the Python-list mailing list