Function to avoid a global variable
Antoon Pardon
antoon.pardon at vub.be
Tue Apr 28 03:39:48 EDT 2020
Op 27/04/20 om 18:39 schreef Bob van der Poel:
> 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?
You can emulate a static variable is with a closure.
def make_parseStack():
depth = 0
def parseStack(inc):
nonlocal depth
if depth > 50:
... report error and die nicely
depth += inc
return parseStack
parseStack = make_parseStack()
--
Antoon Pardon.
More information about the Python-list
mailing list