DEV Community

Cover image for Dead Simple Python: Lambdas, Decorators, and Other Magic

Dead Simple Python: Lambdas, Decorators, and Other Magic

Jason C. McDonald on August 07, 2019

Like the articles? Buy the book! Dead Simple Python by Jason C. McDonald is available from No Starch Press. Python has a reputation for looking...
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 😅

Collapse
 
sandordargo profile image
Sandor Dargo

Very useful article.

Regarding the default depth, it's interesting. Here and there I read 997, then I went to see the CPython implementation, there it's set to 1000.

Well, it doesn't change a lot of things. But probably it's worth mentioning that you actually change that limit, by using sys.setrecursionlimit.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Great insight, thanks!

Collapse
 
ornataweaver profile image
Ornataweaver

There will be no "Transfiguring frog into frog-orange." since you redefined 'partial_transfiguration'.
Thanks for the useful article.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Where specifically is it redefined?

Collapse
 
ornataweaver profile image
Ornataweaver
@party_cannon
def partial_transfiguration(target, combine_with):
    return f"{target}-{combine_with}"

😅

Thread Thread
 
ornataweaver profile image
Ornataweaver

but before this, it was:

def partial_transfiguration(target, combine_with):
    result = f"{target}-{combine_with}"
    print(f"Transfiguring {target} into {result}.")
    return result
Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

Ah, following you now. I've been working in C++ for the past couple of weeks, so "redefined" had a different connotation for me.

I had indeed removed the print statement in the second version! I'll go back and fix that. Thanks for the catch.

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Great explanation of various concepts I see all the time in Python code, but wasn't sure how, or if, I wanted to make use of my own code. Now I can!

Collapse
 
mujeebishaque profile image
Mujeeb Ishaque

I love this guy. Respects.

Collapse
 
ornataweaver profile image
Ornataweaver

Great article, thanks a lot.
I suggest reading dev.to/yawpitch/the-35-words-you-n... to people who like this article.