[Pyrex] Unexpected NameError

Franck Pommereau pommereau at univ-paris12.fr
Thu Nov 22 08:15:19 CET 2007


> That shouldn't make any difference -- global works the same way
> as it does in Python, i.e. you use it inside a function. What
> place in the docs are you referring to? Perhaps I can clarify
> this.

http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/Manual/basics.html#ScopeRules

"You can use a global statement at the module level to explicitly
declare a name to be a module-level name when there would otherwise not
be any indication of this [...]"

I think I was confused by the last words "when there would otherwise not
be any indication of this": I did not know where Pyrex was able to find
this indication. When I re-read the text above, I understand it now:

"Unlike Python, however, a name which is referred to but not declared or
assigned is assumed to reside in the builtin scope, not the module scope."

I'm not sure this is something easy to explain, maybe with a longer example:

print __name__
# prints module's name because __name__ is declared global in f below

print __doc__
# prints __builtins__.__doc__ because __doc__ is never declared global
# 'global __doc__' before the print allows to access module's __doc__

def f () :
    global __name__
    pass

Cheers,
Franck



More information about the Pyrex mailing list