Static variables [was Re: syntax difference]

Stefan Ram stefan.ram at 1
Sat Jun 23 14:32:26 EDT 2018


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

  To: Stefan Ram
From: ram at zedat.fu-berlin.de (Stefan Ram)

ram at zedat.fu-berlin.de (Stefan Ram) writes:
>def f():
>    def g():
>        g.x += 1
>        return g.x
>    g.x = 0
>    return g

  Or, "for all g to share the same x":

  main.py

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

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

g1 = f()
print( g1() )
print( g1() )
print( g1() )

  transcript

1
2
3
4
5
6

-+- 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