DEV Community

Discussion on: Dead Simple Python: Lambdas, Decorators, and Other Magic

Collapse
 
ardunster profile image
Anna R Dunster

Curious about "Functions should not have side effects; they should not modify anything external to themselves." If I had a function designed to take a dictionary as an argument, with the intention that other relevant functions would pass in a dictionary generated dynamically at each call, rather than an existing global, how much does it matter if the function mutates the dictionary to produce its results? (In this case, writing to an SQL database.) Should I bulletproof it, maybe by making a full copy of the input dictionary inside the function, warn about the behavior in the docstring, or does it not matter? I'd be surprised if anyone but me ever uses this code, but, who knows XD

Collapse
 
codemouse92 profile image
Jason C. McDonald

That would still be considered impure, from a functional standpoint. If it makes sense in your code base, you might be able to get away with it, but it's still not recommended, as your unspoken intended rule may not be respected by future-you.

Collapse
 
ardunster profile image
Anna R Dunster

Probably worth doing something to idiot proof it then, who knows who might try to do something with my private project in the future o.o XD Better habit, anyway!

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

Usually the worst future idiots using our projects are our own overworked selves.

Thread Thread
 
ardunster profile image
Anna R Dunster

That's exactly who I'm most worried about 😅