I have a small Python program consisting of very few modules (about 4 or so). The main module creates a list of tuples, thereby representing a number of records. These tuples are available to the other modules through a simple function that returns them (say, get_records()
).
I am not sure if this is good design however. The problem being that the other modules need to know the indexes of each element in the tuple. This increases coupling between the modules, and isn't very transparent to someone who wants to use the main module.
I can think of a couple of alternatives:
Make the index values of the tuple elements available as module constants (e.g.,
IDX_RECORD_TITLE
,IDX_RECORD_STARTDATE
, etc.). This avoids the need of magic numbers liketitle = record[3]
.Don't use tuples, but create a record class, and return a list of these class objects. The advantage being that the class methods will have self-explaining names like
record.get_title()
.Don't use tuples, but dictionaries instead. So in this scenario, the function would return a list of dictionaries. The advantage being that the dictionary keys are also self-explanatory (though someone using the module would need to know them). But this seems like a huge overhead.
I find tuples to be one of the great strengths of Python (very easy to pass compound data around without the coding overhead of classes/objects), so I currently use (1), but still wonder what would be the best approach.