Exception handling with NameError

Peter Otten __peter__ at web.de
Fri Nov 5 18:26:27 CET 2010


Zeynel wrote:

> I want to append new input to list SESSION_U without erasing its
> content. I try this:
> 
> ...
>         try:
>             SESSION_U.append(UNIQUES)
>         except NameError:
>             SESSION_U = []
>             SESSION_U.append(UNIQUES)
> ...
> I would think that at first try I would get the NameError and
> SESSION_U list would be created and appended; the second time try
> would work. But it does not. Do you know why?

If you want SESSION_U to be global you must say so. E. g.:

def your_func():
          global SESSION_U
          # ...
>         try:
>             SESSION_U.append(UNIQUES)
>         except NameError:
>             SESSION_U = []
>             SESSION_U.append(UNIQUES)
          # ...

Of course I'm only guessing because you don't provide enough context.

Peter



More information about the Python-list mailing list