Function to avoid a global variable

Bob van der Poel bob at mellowood.ca
Sun Apr 26 22:46:47 EDT 2020


Does this make as much sense as anything else? I need to track calls to a
function to make sure it doesn't get called to too great a depth. I had a
global which I inc/dec and then check in the function. Works fine, but I do
need to keep a global around just for this.

So ... instead I wrote a short function:

 def parseStack(inc, depth=[0]):
     if depth[0] > 50:
         ... report error and die nicely
    depth[0] += inc

This gets rid of the global, and it moves my error check out of the main
code as well. But, I never feel all that great about using a list in the
function call for static storage.


More information about the Python-list mailing list