I’ve read that you should never test for the existence of a variable; if your program has to check whether a variable exists, you don’t “know your variables” and it is a design error. However, I have a recursive function that adds values to a dictionary and a list during each call to the function. In order to avoid declaring global variables, I am trying to make the variables local to the function. But in order to do that, I have to declare myList and myDict as [] and {} in the beginning of the function. Of course, that erases the changes I made to the dict and list in the previous recursive calls, which I don’t want. I thought about instating a try … catch at the beginning, checking for the existence of the variables, and only declaring them as {} and [] if they do not yet exist, but I’ve read that is bad design. Is there a better way to approach this? I apologize for not attaching any actual code, but I’m still at the beginning stages of planning this function, so there is nothing much to attach.
Share
If you really need access to mutable state in your recursive function, you should probably use a class.
In situations like this, using languages that don’t support object-oriented programming (OOP), the best approach is often simply to use global variables, possibly contained within their own module to avoid namespace collisions. Global variables are “bad,” but sometimes they are the lesser of two (or more) evils. But you’re writing code in Python, which provides you with a rich set of OOP constructs. You should use them.
For example, here’s a simple recursive Fibonacci function that uses memoization to avoid the O(2^n) complexity of the naive version (note that the leading underscore just means that the name is “private” and shouldn’t be used without detailed knowledge of its inner workings):
A quick test; note that the second call returns with no perceptible delay:
You can see that this “function” (really a callable object) solves the problem you describe by creating the mutable object when it is created. Then it accesses the mutable object via
self. There are better ways to writefibthan the above, but this is likely to be a good basic design for more complex recursive functions requiring state.Of course you have many other options. Using a mutable default as mentioned by another answer is quite similar — but I prefer using a class, because it’s much more explicit. You could even store the objects as attributes of the function!
I don’t love this construction but it’s worth knowing about. Finally, don’t completely disregard the most obvious solution:
Sometimes this is actually the best way! It’s certainly simple and explicit.
As a final note, I will observe that you seem to be misunderstanding the nature of function scope. You say
This isn’t bad design. It’s broken design — it just won’t work. If your dictionary and your list aren’t global, then your
try/exceptwill always raise an exception, because at the beginning of a function, no local variables are defined.This leads me to a final thought. Perhaps you want to create a new dictionary and list every time the function is initially called, and throw them away after the recursion stops. The class-based option above would have to be tweaked in that case; or you could just wrap your recursive function in an outer function that creates them anew every time it is called: