Guilty secret: globals baffle me

TeaAndBikkie teaandbikkie at aol.com
Wed Nov 6 17:51:02 EST 2002


>From: Gonçalo Rodrigues op73418 at mail.telepac.pt 
>
>On 6 Nov 2002 17:21:46 GMT, bokr at oz.net (Bengt Richter) wrote:
>
>>I have an uncomfortable feeling there is unfinished
>>business in the way names in nested scopes and otherwise
>>related namespaces work. The global/local dichotomy doesn't
>>fully express the possibilities any more. IMO it would be nice
>>to have full access to accessible namespaces without magically
>>determined access mode restrictions based on full-function lookahead.
>>
>>I.e., we have a de facto hierarchy of name spaces defined by the
>>ability to nest functions. Why not have an access mechanism that
>>allows rebinding as desired? E.g., you could let locals take a depth
>argument,
>>so locals() is equivalent to locals(0), and locals(1) is the enclosing
>>name space, so locals(1)['x'] = 3 in bar above would rebind the x local to
>foo.
>>
>
>I totally agree with you. The uncomfortable feeling is underlined by the
>fact that you can rebind global names through the use of global (the
>only declaration in Python - ach!) but not names in the enclosing scope
>- if it is not global that is. Although I do not feel that there is a
>need for this feature in Python, it strikes as an ugly asymmetry - the
>ugliest in fact.
>
>I would just change a little bit your notation. Instead of reusing
>locals use something like
>
>enclosed()
>
>returning the enclosing scope. And you've guessed it - if you want to
>get at the next scope just use enclosed().enclosed(). The enclosed() at
>global scope should return None.
>
>Now if you wanted to rebind a variable in the enclosing scope you should
>be able to use the notation
>
>enclosed().x = <whatever>
>
>and not dict notation. globals() should follow the same rules.

I would like to be able to have named scope, so you can specify precisely from
which scope the variable is in. Perhaps mixed with relative scope indexing? eg:

def eric():
    licence='fish'
    def bruce():
        licence='halibut'
        print scope(eric).licence
        print scope(-1).licence

Or something more pythonic :-) 

-- Misha




More information about the Python-list mailing list