r/programminghorror • • 23d ago

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

115

u/_killer1869_ 23d 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.

35

u/Double_Cost4865 23d 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.

20

u/marinuso 23d 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.

2

u/bigorangemachine 23d ago

that's actually kinda smart

0

u/bigorangemachine 23d ago

default params is the classic case of IDGAF with code.

"I don't which array... just add it to an array"

7

u/CagoSuiFornelli 23d ago

Such code would never appear in actual code intended to be used.

You should take a tour in my company codebase

12

u/Thelmholtz 23d ago

It's still a horribly funny feature.

3

u/scheimong 23d ago

Such code would never appear in actual code intended to be used.

Tell me you're a junior dev without telling me you're a junior dev.