question about scope

Fuzzyman fuzzyman at gmail.com
Thu Feb 16 12:00:27 EST 2006


John Salerno wrote:
[snip..]
>
> Thanks guys. It seems like nested functions were what the authors had in
> mind, so that makes a lot more sense now.
>
> But as far as ifs and loops, is there such a thing as scope in them? For
> example, if I assign a variable within an if statement, is it usable
> anywhere else?

Defining a variable in a loop, or within an 'if block' doesn't change
scope, so the variables are useable in the same way as variables
outside the loop. Obviously if you define them within an if block, they
may *not* be defined, so using them could raise a NameError.

if False:
    test = 'something'
print test

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list