Functions continuing to ru after returning something?

Aidan aweraw at gmail.com
Mon Aug 30 23:28:23 EDT 2010


Bradley Hintze wrote:
> I may be having a brain fart, but is it at all possible to have a
> function first return a value then continue its calculation. Like this
> simple example:
> 
> my_var = 5
> def my_function():
>     return my_var
>     my_var +=1
> 
> This obviously won't work as written but is there a cleaver way around this.
> 

def my_function():
     my_var = 5
     while my_var <= 10:
         yield my_var
         my_var += 1

 >>> for x in my_function():
...     print x
5
6
7
8
9
10
 >>>





More information about the Python-list mailing list