[Tutor] any cons to using a module of functions?

Alan Gauld alan.gauld at btinternet.com
Fri Feb 3 09:59:38 CET 2012


On 03/02/12 05:05, Che M wrote:

> is very bad form and I should refactor, and so I am beginning to put
> these functions in their own module so that I can import the module and
> its functions when I need it; they will all be in one place and only
> only place.

While that's tempting it is better if you use multiple modules such that 
the functions in them are related in some way. A single mixed bag of 
functions will eventually become messy to maintain. Even if some modules 
only contain a single function its a lot clearer than having a "bag of bits"

> My question is about resources. Let's say I have the module, myUtils.py,
> and I import it into every other module that will need one or more of
> the functions within it. Is this in any way costly in terms of memory?

Not really, Python creates one instance of the module and all the 
importing modules refer to that instance. The only way it's wasteful is 
if you have 20 functions and only need two then you have 18 function 
objects that you don't need. (see the point above about multiple 
modules!) But even then the memory usage is unlikely to be a major issue 
since 18 function objects will generally consume minimal memory
on a modern PC.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list