On Friday, April 4, 2014 5:51:11 PM UTC-4, Cliff Kachinske wrote:
>
> If I write a python module like this:
>
> # born_to_fail.py
> foo = 'bar'
> def main():
>     print foo
> if __name__=='__main__': main()
>
>
The above shouldn't produce an error, but the following will:

foo = 'bar'
def main():
    print foo
    foo = 'foo'

In the above, the assignment to foo indicates foo is a local variable 
(since it hasn't been explicitly declared as global), so the previous print 
statement raises an exception because it refers to a local variable that 
has not yet been declared.

In your original example, because there is no assignment to foo within the 
function, the foo in the print statement is assumed to be global, and there 
is no exception because foo is in fact a global variable.

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to