Reasoning behind nested scope

Mel Wilson mwilson at the-wire.com
Tue Aug 3 15:45:04 CEST 2004


In article <mailman.1087.1091532385.5135.python-list at python.org>,
"Andy Baker" <andy at andybak.net> wrote:
>"This is because nested function definitions don't have access to the
>local variables of the surrounding block -- only to the globals of the
>containing module. This is done so that lookup of globals doesn't
>have to walk a chain of dictionaries -- as in C, there are just two
>nested scopes: locals and globals (and beyond this, built-ins).
>Therefore, nested functions have only a limited use. This was a
>deliberate decision, based upon experience with languages allowing
>arbitraries nesting such as Pascal and both Algols -- code with too
>many nested scopes is about as readable as code with too many GOTOs.
>And, of course, in Python, the "proper" way is to use an
>object-oriented programming style"
>
>This sounds reasonable to me although nested scope always struck me as more
>natural and intuitive ('Don't be surprising')
>
>I was wondering how the balance changed in favour of nested scope? What
>changed people's minds about 'readability' vs other factors? Are nested
>scopes still regarded as leading to spagetti code etc?

   I guess a consensus built up that the power/safety
tradeoff had given too much emphasis to safety.

   My own poster child for nested scopes is in some old post
on c.l.p . I wanted to build a lot of similar labelled text
controls in wxPython, following a naming convention.  I
could have written a function, but pre-nested-scope, any
change I made to the information used in building the
controls would have meant changing the function parameter
list, and all the function calls.  I came up with a template
string which got parameter substituted via the '%' operator,
and then `exec`ed so as code it had full access to all the
info that was in the contemporary scope.

   Post nested scope, any info the function needed from the
surrounding scope could simply be read from there.  The code
was much less unusual.

   I believe (without proof) that any language that won't
let me shoot myself in the foot will also prevent me doing
anything useful.  Debate is heating up now about write
access to enclosing scopes.  We'll see how strong my faith
really is.  People will be able to write function nests
instead of classes.

>(On a side note is there any way to call a nested function from outside the
>parent? I was kind of expecting nested functions to be addressable through
>dot notation like methods are but I can see why that wouldn't be quite
>right. This might be a better question for the tutor list...)

Yup.

    def f1 (a):
        if a:
            x = 2
        else:
            x = 3

        def f2 (y):
            return y % x

        return f2

    import os
    b = f1 (os.name.startswith ('p'))
    for c in range (10):
        print b (c)



                Regards.        Mel.



More information about the Python-list mailing list