Chapter 5. Classes

Simple collections are appropriate for organizing relatively simple and transient information. Elaborately nested collections, though, are not. Consider the output parsed from the very small GenBank entry shown in Example 4-31. Even though it ignores much of the file’s information, the program produces an elaborate nested structure. Working with such a structure creates the following serious problems:

Complexity

Using the representation in a program requires understanding its intricacies.

Awkward navigation

Accessing specific parts of the representation requires tricky combinations of index expressions and function calls.

Exposure to change

Changes to the representation require widespread changes in every program that uses it.

The combination of these factors causes mental strain, slows programming, makes debugging more difficult, and increases mistakes. It is too easy to write [1] instead of [0], or [1] instead of [0][1]. Later, such index expressions have no apparent meaning, so they are difficult to understand and correct. If changes must be made it is difficult to make them consistently and find every place that must be edited. While collections are powerful tools, they are best reserved for actions that operate on the entire collection, rather than being used with access expressions or functions that pick out specific elements.

These problems arise because of the entanglement of code that does need to know about representation details and code that does not. For ...

Get Bioinformatics Programming Using Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.