Static variables [was Re: syntax difference]

Stefan Ram stefan.ram at 1
Sat Jun 23 14:29:24 EDT 2018


  To: Steven D'Aprano
From: "Stefan Ram" <stefan.ram at 1:261/38.remove-r7u-this>

  To: Steven D'Aprano
From: ram at zedat.fu-berlin.de (Stefan Ram)

Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>def f():
>    static x = 0
>    def g():
>        x += 1
>        return x
>    return g

  What one can do today:

  main.py

def g():
    g.x += 1
    return g.x
g.x = 0

print( g() )
print( g() )
print( g() )

  transcript

1
2
3

  main.py

def f():
    def g():
        g.x += 1
        return g.x
    g.x = 0
    return g

g = f()
print( g() )
print( g() )
print( g() )

  transcript

1
2
3

-+- BBBS/Li6 v4.10 Toy-3
 + Origin: Prism bbs (1:261/38)

--- BBBS/Li6 v4.10 Toy-3
 * Origin: Prism bbs (1:261/38)



More information about the Python-list mailing list