Function to avoid a global variable

jfong at ms4.hinet.net jfong at ms4.hinet.net
Tue Apr 28 01:49:59 EDT 2020


bvdp於 2020年4月28日星期二 UTC+8上午9時46分35秒寫道:
> Oh my, that is very cool! So, I can do this:
> 
> def foo(i):
>     if not 'bar' in foo.__dict__:
>         foo.bar = 5
>     foo.bar += i

You can have function attribute created this way if you like:

    def foo(i):
        foo.bar += i
    foo.bar = 5

--Jach



More information about the Python-list mailing list