First script, please comment and advise
just at xs4all.nl
Thu Mar 9 18:27:19 CET 2006
In article <1141920446.935210.260650 at j52g2000cwj.googlegroups.com>,
bearophileHUGS at lycos.com wrote:
> > Btw. I find the use of a nested function here completely bogus: you
> > don't need the surrounding scope.
> I don't agree, nested functions are useful to better structure your
> program: to really nest things that are logically nested, to have just
> one thing when you have only one thing to do, and they help you to
> avoid polluting the namespace (those were the main purposes of nested
> functions in Pascal-like languages).
Luckily this is Python, not Pascal :)
1. The fact that one function calls the other doesn't mean they're
2. The helper function is useful on its own, so it is a waste to hide it
3. The nested function makes the enclosing function harder to read.
4. I don't buy the namespace pollution argument, we have modules after
5. Overhead: every time a new function object is created, for no good
reason. (But you save a global name lookup, yet that doesn't appear to
weigh up against the function def. [*])
To me, nested functions (in Python) are *only* helpful when using
closures. If you're not using the enclosing scope, you're just
% python2.4 -m timeit -s "def helper(): pass" "def nonnesting():" \
" helper()" "nonnesting()"
1000000 loops, best of 3: 1.92 usec per loop
% python2.4 -m timeit "def nesting():" " def helper(): pass" \
" helper()" "nesting()"
100000 loops, best of 3: 2.04 usec per loop
More information about the Python-list