Why a class when there will only be one instance?

Dave Harris dpharris76 at msn.com
Wed May 26 10:34:31 EDT 2004


Peter Maas wrote:
>
>>SeeBelow at SeeBelow.Nut wrote:
>>Even easier is not to make anything a class unless there will be two or
>>more instances of it.  I still don't get what advantage making a class
>>buys for you.
>
>To use classes even in single instance cases has some advantages:
>
>- There is a unique way of organizing your code.
>
>- There is an easy transition to the multiple instance case.
>
>- It makes writing meta code (e.g. for documentation, transformation
>   ...) easier.

That's the key issue for me (meta-code), and it's driven by my tools. I'm 
using Leo to organize my project and the class/OO paradigm lends itself 
extremely well to Outlining. Each class naturally forms a node in the tree, 
giving me an easy way to expand and contract the code.

For example (to drag this tired code out of the dark again) :

- class App
|  - __init__
|  - loadINI
- class CourseView
|  - __init__
|  - drawAll
|
- class DetailView
|  - __init__
|  - drawAll

I could shoehorn a function-only design into a tree structure, but the 
functions are not nested and it just doesn't feel comfortable.

Is it appropriate to let your tools affect your design? It depends. If you 
are more productive AND the artifacts make the product more maintainable, I 
willing to play along with it.

Dave Harris






More information about the Python-list mailing list