Why a class when there will only be one instance?

François Pinard pinard at iro.umontreal.ca
Thu May 27 18:41:17 EDT 2004


[Christian Tismer]
> François Pinard wrote:

> >It would be `self.' within the class itself, and the instance name
> >within foreign classes.  This habit stresses the relations between the
> >parts of the program and eases a better understanding of the whole.

> Just a little observation:  If you really want the class and the
> instance to be the same, well, you can take a module.  In a sense,
> this *is* a class and the only instance.

Indeed, and for programs which are more than small, this is a very
natural way to proceed.  On the other hand, for programs which are not
so big, it is often quite practical to keep everything in a single file,
and there, short of having many modules, sole instances of a few classes
are still an attractive way for organising the code.

It might be worth noting, as a kind of counter-argument, that abusing of
classes may decrease legibility instead of increasing it.  As usual in
such things, sense of measure, and good taste, are always welcome! :-)

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list