Function to avoid a global variable

Michael Torrie torriem at gmail.com
Mon Apr 27 20:31:24 EDT 2020


On 4/27/20 10:39 AM, Bob van der Poel wrote:
> Thanks Chris!
> 
> At least my code isn't (quite!) as bad as the xkcd example :)
> 
> Guess my "concern" is using the initialized array in the function:
> 
>    def myfunct(a, b, c=array[0,1,2,3] )
> 
> always feels like an abuse.
> 
> Has anyone seriously considered implementing  a true static variable in a
> function? Is there a PEP?

There's actually no need. You can create attributes on the function
itself, just like a regular object:

def foo():
    if not 'bar' in foo.__dict__:
	foo.bar = 5

    print (foo.bar)




More information about the Python-list mailing list