Functions continuing to ru after returning something?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Aug 31 04:18:56 EDT 2010


Peter Otten a écrit :

>>>> n = 1
>>>> def f():
> ...     global n
> ...     try:
> ...             return n
> ...     finally:
> ...             n += 1
> ...

The same without a global:

def f(_n=[0]):
     try:
         return _n[0]
     finally:
         _n[0] += 1


But yeps, using a generator would be better.



More information about the Python-list mailing list