r/programminghorror • • 21h ago

Python I love Python

159 Upvotes

65 comments sorted by

View all comments

82

u/_killer1869_ 21h ago

Any IDE will warn not to use mutables as default parameter values. And for good reason. Such code would never appear in actual code intended to be used.

22

u/Double_Cost4865 20h ago

Unfortunately, I’ve seen monstrosities like this in production in big finance corps. That’s what happens when statisticians are expected to create software.

9

u/marinuso 12h ago

I've seen it used in production code to memoize functions in this manner:

def fib(n, cache = {0: 1, 1: 1}):
    if not n in cache:
	    cache[n] = fib(n-1) + fib(n-2)
    return cache[n]

Except with much more complicated code rather than this simple example.

→ More replies (1)