[Tutor] Python structure advice ?

Kent Johnson kent37 at tds.net
Thu Dec 16 20:08:24 CET 2004


Dave S wrote:
> Dave S wrote:
>> The 'remembering where is was' seems a continuous stumbling block for 
>> me. I have though of coding each module as a class but this seems like 
>> a cheat. I could declare copious globals, this seems messy, I could 
>> define each module as a thread & get them talking via queues, given 
>> this serious thought but heeded warning in previous posts. I have 
>> thought about returning an list of saved 'pointers' which would be 
>> re-submitted when the function is called. I don't know which way to turn.
>>
> Having written this email, it has put my thoughts in order, though it 
> seems a bit cheaty, wouldn't defining all modules that have to remember 
> their internal state as classes be the best bet ?
> 
> Dave

Why do you say this is 'cheaty'? A class is basically a collection of data (state) and functions to 
operate on that state.

You might be interested in this essay:
http://www.pycs.net/users/0000323/stories/15.html

It might well make sense to organize your program as a collection of cooperating classes, or maybe a 
collection of classes with a top-level function that stitches them all together.

You might also want to learn about iterator classes and generator functions, they are a technique 
for returning a bit of data at a time while maintaining state. You might be able to structure your 
input stage as an iterator or generator.
http://docs.python.org/tut/node11.html#SECTION0011900000000000000000
http://docs.python.org/lib/typeiter.html

Kent


More information about the Tutor mailing list