Hey all the interviewers out there. What are your thoughts on a candidate who uses Python closure incorrectly as nested function to update states.For example I see lot of people define dfs function in their top function and use variables from enclosing function. Something like this def solve(): visited = [0] * 10 def dfs(u): visited[u] = 1 dfs(u+1) dfs(0) Also if you can share some suggestions on what not to do while using Python in interviews. TC 190k Yoe 11
overflow is worse than closure variable
What exactly is incorrect about using nested functions like that? Seems fine to me
Same question, I’ve been using this all the time, maybe I’m an idiot.
You have to declare visited as nonlocal in the dfs function in order to modify it
Probably best to have it in its own function (as the idea is that DFS is fairly reusable), but that’s this case in particular, not closures in general. Overall it’s fine — the big benefit is that they can improve code readability so use appropriately