exec "statement" VS. exec "statement" in globals(), locals()

Michel Claveau, résurectionné d'outre-bombe informatique unseulmcmcmcmc at msupprimerlepoint.claveauPOINTcom
Wed Jul 21 16:41:21 EDT 2004


Hi !


In the syntax 2,  because   "ret=2",  "ret" is local to f()




Try :

chaine="""global ret
ret=ret+10"""

def f():
    global ret
    ret = 2
    exec chaine in globals(), locals()      #now, idem  exec(chaine)
    return ret

print f()  #==> 12







More information about the Python-list mailing list