I was trying to solve a problem consisting in finding the max value of a list with several level of depth. I tried several ideas but no luck. So I found this online. I actually had this idea myself but I discarded it without testing it for the reason I'll mention right after.
def get_max(my_list):
m = None
for item in my_list:
if isinstance(item, (list, tuple)):
item = get_max(item)
if not m or m < item:
m = item
return m
Sample run:
>>> get_max(((10, (1,2), [[1],[9]])))
>>> 10
So I did discard this idea because I thought that if the value of m is reset to None at each recursive steps, it's not going to find the max value I need. I ran it in Python Tutor but I still don't understand how m would remember the value 10 as it as been reset to None several times.
Could somebody explain to me please?
Does every recursive step create a new frame somehow?