[Tutor] question about function inside of function

Richard D. Moores rdmoores at gmail.com
Sat Jan 9 18:11:03 CET 2010


On Sat, Jan 9, 2010 at 07:28, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "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.

That's OK. It's very specific to the enclosing 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!

I moved it to the top. But since writing it I debugged and edited the
script so that the small function is used in only one case. So you're
right, I don't need it as a function at all.

Thanks, Alan. Right on as usual.

Dick


More information about the Tutor mailing list