Question about "exec in globals, locals"

Adrien Di Mascio Adrien.DiMascio at logilab.fr
Fri Jul 4 06:51:56 EDT 2003


Hi,
> source = """
> class FirstClass:
>     pass
> class SecondClass:
>     References = [FirstClass]
> """


When you specify both locals() and globals(), here's what happen :

"""
class FirstClass:
    pass

## ---------> Here, your locals() dict have been updated, but not your
## globals() one.
## But, here, you can do : References = [FirstClass] since your
## locals()
## know about 'FirstClass'

class SecondClass:
    ## ---------> Here, your locals() dict is a new one since you've
    just
    ## entered a new scope. So, 'FirstClass' is neither defined in
    ## 'locals()' nor in 'globals()', that's why you have your
    NameError
"""

I'm not quite sure of my explanation, but that could explain your
problem. Sorry if I've made any Python mistake.

Cheers,

-- 
Adrien Di Mascio
LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr   http://www.logilab.org






More information about the Python-list mailing list