Function to avoid a global variable

Tony Flury tony.flury at btinternet.com
Wed Apr 29 07:03:49 EDT 2020


On 28/04/2020 06:49, jfong at ms4.hinet.net wrote:
> 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
>
And as you have shown - foo.bar is effectively a global variable - just 
one with a qualified name :-)


-- 

Tony Flury



More information about the Python-list mailing list