[Tutor] question about function inside of function

Alan Gauld alan.gauld at btinternet.com
Sat Jan 9 16:28:55 CET 2010


"Richard D. Moores" <rdmoores at gmail.com> wrote 

> to be put in a function. For convenience sake, I've put this new
> function inside the one that calls it.
> 
> Question 1: Is this bad practice? It works fine that way, but..

No, but there are some issues to consider.
Denis has addressed some but one other is...

Reuse: by hiding the function inside the outer one it means it 
can only ever be used inside that function. It may be better to 
have it as a module level function - even if you use the _prefix 
to limit its use outside the module.

> Question 2: If the answer to Q1 is no, is there a standard place to
> put a function inside of another function? Is it standard to have it
> the first line of the function? I've put mine at the point where it
> would be called, mainly as a reminder to myself.

Top of the function is best because as Denis says it only gets 
defined once per function call. If you are really paranoid and 
the function only gets used in exceptional cases then you could 
define it at the point of use but in that case you probably don't 
need a function at all!

HTH,


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



More information about the Tutor mailing list