[Python-de] funktionen und statische variablen

Juergen Hermann jh at web.de
Wed Sep 18 20:53:36 EDT 2002


On Tue, 17 Sep 2002 17:43:08 +0200, Rainer Fischbach wrote:

>ist der Versuch, in Python nachzubilden, was z. B. in Scheme ganz einfach
>so geht:
>
>(define f (let ((c 0)) 
>            (lambda (x) (set! c (+ c 1)) (* c x))))
>
>> (f 1)
>1
>> (f 1)
>2
>> (f 1)
>3

>>> from __future__ import generators
>>> def f(start=1, inc=1):
...   while 1:
...     yield start
...     start += inc
...
>>> g = f(3,2).next
>>> g()
3
>>> g()
5
>>> g()
7



Ciao, Jürgen






More information about the Python-de mailing list