First script, please comment and advise

Just just at xs4all.nl
Fri Mar 10 19:30:16 CET 2006


In article <1141936277.648924.119020 at z34g2000cwc.googlegroups.com>,
 bearophileHUGS at lycos.com wrote:

> >1. The fact that one function calls the other doesn't mean they're 
> >"logically nested".<
> 
> In this situation they were logically nested, because the scramble_word
> is a function needed by another one only, in Java the scramble_word
> becomes a (private) method of a text scrambling class; this means it is
> nested and hided inside a class. Pascal languages were pre-OOP and they
> used nested function for similar purposes, that it to hide and
> structure functions inside other ones, that in OO languages like Java,
> ObjectPascal, Python, etc. you usually do with classes.

In Python one usually doesn't try to hide things.

[ ... ]

> >4. I don't buy the namespace pollution argument, we have modules after all.
> 
> I presume Guido agrees that function nesting is another way to avoid
> namespace pollution :-)

I dare you to find out. I'm fairly sure of the answer.

> There are many ways to do the same thing, to
> avoid pollution in one module.

Nesting functions is not one of them. Factoring a bit of functionality 
into a helper function, giving that function a name and sticking it into 
a module can _hardly_ be called "pollution". That's what modules are 
*for*.

[ ... ]

> >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.<
> 
> Note that until recent time, Python nested scopes were managed quite
> differently (in a bad way),

Yup.

> and yet, they regarded the function nesting
> useful enough to be kept anyway.

Nested functions are supported because you _need_ them if you want to 
work with closures. Not because it's a good way to structure code in the 
*absence* of closures.

> I don't agree with you, but every programmer is diffent, because their
> story is different. If you have learnt to program with Java/C then you
> probably don't like nested functions. If you started with a Pascal-like
> language then probably you like that nesting more. If you start with a
> Scheme/Lisp background you progrably write Python programs like the
> good Norvig:
> http://aima.cs.berkeley.edu/python/utils.py

That's pretty good code. Well documented, clearly written, uses some 
functional idioms in a sensible way. And no, it doesn't use gratuitous 
nested functions.

> Maybe other people here can say what they think about this topic, it's
> just a relaxed discussion.

Sure, it's just that I feel strongly that your advice is not a good one 
(*especially* for beginners).

So I repeat: using nested functions are NOT a good way to factor your 
code. (I don't need to add "in Python" there, do I?)

Just



More information about the Python-list mailing list