Base classes or global functions

Alex Martelli aleax at aleax.it
Sun Jul 21 13:41:05 EDT 2002


<posted & mailed>

Stephen Boulet wrote:

> I have two classes, Class1 and Class2. Class1 contains a list of Class2
> objects, but no inheritance is involved
> 
> Both classes use some functions I wrote. What's the best way of making the
> functions available to the classes?
> 
> -- Stephen, who's trying to develop good programming habits

Put those functions you wrote into their own module, say myfuns.py.  Import 
that module from each of the modules implementing Class1 and Class2, at
the start of the module, i.e., have:

import myfuns

early in each of the class1.py and class2.py modules.

Then whenever in those importing modules you need to call a given
function, say, givefu, defined in myfuns.py, call myfuns.givefu().


Alex




More information about the Python-list mailing list