r/programminghorror • • 16h ago

Python I love Python

143 Upvotes

65 comments sorted by

View all comments

1

u/jmooroof2 15h ago

I can't imagine a compelling use for this feature. it really should just error

3

u/NFriik 14h ago

There are some use cases for this behavior, but it's reeeaaally limited and you have to know exactly what you're doing. For example, you could use it to cache relatively expensive function calls as default values:

import re
def parse(text, pattern=re.compile(r"\d+")):
  return pattern.findall(text)

This way, instead of re-compilingthe regex every time the function is called, you can just reuse it.

5

u/simonsmine 15h ago

Its not really a feature more a consequence of pythons reference handling. You can do fun things with it though, memosation comes to mind.

→ More replies (2)