[Tutor] OT How to approach problem ?

Dave S pythontut at pusspaws.net
Mon May 24 03:47:42 EDT 2004


Alan Gauld wrote:

>>Having written a program to track the ftse250, I have two large
>>    
>>
>lists of
>  
>
>>dictionaries forming 2 x 'fluid' matries. To access this data I have
>>various defs which all use common pointers into the matries.
>>    
>>
>
>Probably twoi clases or maybe just two instances of one class
>- since they are both lists of dictionaries they presumably
>share common behaviour?
>  
>
The second matrix is a deep copy of the first at the end of a trading 
day so stock information
for two days is retained. Statistical info is only generated & stored in 
the first matrix, then copied.

>  
>
>>OK this works, but apparently globals are to be avoided.
>>    
>>
>
>In principle yes, but...
>
>  
>
>>The only other way I can see is to define a class, and use
>>    
>>
>self.pointer
>  
>
>>instead of global pointers .... Since there would only be
>>one instance of this class this seems a strange way to approach it.
>>    
>>
>
>One instance of a class is just hiding global variables. Might still
>be
>better because who knows when you find a need for multiple instances
>(maybe even in another program 9in the future...). But generally a
>single instance is just wallpaper over globals.
>  
>
 ... this confirms something I suspected.  Turning it into a one 
instance class would work but
seems a bit fruitless ...

>The other normal approach is to pass the pointers in as parameters of
>the functions. This might mean a lot of parameters but intelligent
>use of default values can often make it manageable and make the
>functions much more reusable.
>  
>
I was concerned about speed, I would be passing a lot of data through 
another layer of def. Scanning the matrix via another
function which would be called 110*250*12=330,000 times a throw.

I guess the only way to find out the time penalty is to code it & time 
it :-)

I appreciate your help, Although I 'bodged' it with globals It is good 
to see other alternatives.

Cheers
Dave

>HTH,
>
>Alan G
>Author of the Learn to Program web tutor
>http://www.freenetpages.co.uk/hp/alan.gauld
>
>
>
>  
>




More information about the Tutor mailing list