I am using a recursive function to sort a list in Python, and I want to keep track of the number of sorts / merges as the function continues. However, when I declare / initialize a variable inside a function, it becomes a local variable inside each subsequent function call. If I declare a variable outside the function, the function considers that it does not exist (i.e. Does not have access to it). How can I share this value with different function calls?
I tried using the "global" variable tag inside and outside the function as follows:
global invcount
def inv_sort (listIn):
global invcount
if (invcount == undefined):
invcount = 0
But I cannot figure out how to check the undefined status of a global variable and assign it a value for the first recursion call (because in all consecutive recursions it must have a value and be defined).
, , , , . invcount , , "i27". ( i ) () . , , , , .
/?