[Python-Dev] just trying to catch up with the semantic

Samuele Pedroni Samuele Pedroni <pedroni@inf.ethz.ch>
Thu, 1 Mar 2001 17:53:43 +0100 (MET)


Hi.

Your rationale sounds ok.
We are just facing the oddities of the python rule - that assignment
indetifies locals - when extended to nested scopes new world.
(Everybody will be confused his own way ;), better write non confusing
code ;))
I think I should really learn to read code this way, and also
everybody coming from languages with explicit declarations:

is the semantic (expressed through bytecode instrs) right?

(I)
    from __future__ import nested_scopes
    x=7
    def f():
        #pseudo-local-decl x
        x=1
        def g():
            global x # global-decl x
            def i():
                def h():
                    return x # => LOAD_GLOBAL
                return h()
            return i()
        return g()
    
    print f()
    print x

(II)
        def g():
            #pseudo-local-decl x
	    x = 2            # instead of global
            def i():
                def h():
                    return x # => LOAD_DEREF (x from g)
                return h()
            return i()

(III)
        def g():
            global x # global-decl x
	    x = 2 # => STORE_GLOBAL
            def i():
                def h():
                    return x # => LOAD_GLOBAL
                return h()
            return i()
(IV)           
    def f():
        # pseudo-local-decl x
        x = 3 # => STORE_FAST
        def g():
            global x # global-decl x
	    x = 2 # => STORE_GLOBAL
            def i():
                def h():
                    return x # => LOAD_GLOBAL
                return h()
            return i()
(IV)
        def g():
            global x # global-decl x
	    x = 2 # => STORE_GLOBAL
            def i():
                def h():
                    # pseudo-local-decl x
                    x = 10   # => STORE_FAST
                    return x # => LOAD_FAST
                return h()
            return i()
If one reads also here the implicit local-decl, this is fine, otherwise this 
is confusing. It's a matter whether 'global' kills the local-decl only in one
scope or in the nesting too. I have no preference.


regards, Samuele Pedroni.