Re: [Tutor] any cons to using a module of functions?
Che M wrote: I have a bunch of functions that do various utility-type tasks in an application (such as prettifying date strings, etc.), and they are used in many modules. Much of the time, I have just been lazily copying and pasting the functions into whichever modules need them. I realize that 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. 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? (since each time I import myUtils.py I import *all* the functions, instead of in the cutpaste approach, where I just run the functions I need). I hope by importing all functions you mean import myutils or from myutils import foo, bar The oh-so-convenient from myutils import * will sooner or later result in nasty name clashes. I'm fairly sure this is not at all an issue, but I just want to understand why it's not. After entering the interactive interpreter (Python 2.7) I see import sys len(sys.modules) 39 len(sys.builtin_module_names) 20 So there are already forty or sixty modules, depending on how you count; the memory and runtime impact of adding one more is likely negligable. There is an effect on your processes. If you hammer up a quick and dirty script using your kitchen-sink myutils.py, then forget the script, and in a year or so need it again it's likely that myutils have evolved and your script will not work out of the box. At that point it will start to pay off having unit tests in place that ensure a stable api and to put everything into version control. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] any cons to using a module of functions?
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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] any cons to using a module of functions?
I have a bunch of functions that do various utility-type tasks in an application (such as prettifying date strings, etc.), and they are used in many modules. Much of the time, I have just been lazily copying and pasting the functions into whichever modules need them. I realize that 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. 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? (since each time I import myUtils.py I import *all* the functions, instead of in the cutpaste approach, where I just run the functions I need). I'm fairly sure this is not at all an issue, but I just want to understand why it's not. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor