In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] wrote:

> Just:
> 
> > 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 
"logically nested".

2. The helper function is useful on its own, so it is a waste to hide it 
inside another.

3. The nested function makes the enclosing function harder to read.

4. I don't buy the namespace pollution argument, we have modules after 
all.

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 
obfuscating things.

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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to