namespaces & indentation

Chris Schaller Chris.Schaller at web.de
Fri Dec 22 15:56:04 EST 2000


Dear Pythoners...

  I always thought that I understood Python's scoping, but then I came across
that code:
> def t(x):
>   if x == 1:
>     c = 'yeah'
>   return c

If I execute t(1), I'll get 'yeah', but otherwise there'll be an error
message.  According to the indentation level c is only valid within the
if-clause, but not on the outside.  So I guess, the indentation level only
defines the lines of code that belong to the if-clause, but has nothing to do
with scopes.

  That's very confusing, I've already programmed a lot in Python, but only
due to a program error (I forgot to initialize c at the beginning of the
function - a common error) and an unsuccessful test this happened to a program
after months of error-free running.

  Any comments?

bye
  Chris...



More information about the Python-list mailing list