Things (Almost) No One Thinks About When Designing Functions in Python
110K views · Sep 20, 2024 · Education
Comments · 196
@ArjanCodes · 2 years ago · pinned
💡 Get my FREE 7-step guide to help you consistently design great software: <a href="https://arjancodes.com/designguide">https://arjancodes.com/designguide</a>.
2
@metal571 · 2 years ago
"Make interfaces easy to use correctly and hard to use incorrectly."<br>- Scott Meyers, The Most Important Design Guideline
83
@saitaro · 2 years ago
There's another naming style sometimes used in the Python stdlib and ecosystem: adjectives describing the quality of the returned object, like reversed, sorted and functools.batched. They do not change the argument, so a name like "sort" would be confusing in this case, it's used for the corresponding method, which actually transforms the object.<br> I don't know how this style is named though.
14
@rycona-yt · 9 months ago
In my heart, I believe "kwargs" is the sound a coding chocobo (chocodebo) makes.
2
@DrGreenGiant · 2 years ago
An approach I use is by going a level up to the function that is calling the one I'm about to write. In that calling function, the name and arguments should read in plain English, almost like a story. Ultimately, if you've got your face in a function you can see what it does, but if you are in the outer scope, a good signature means you don't need to dig deeper on that line to reason about what's going on. <br>Great video, thank you!
49
@yvesdeutschmann9899 · 2 years ago
Great video. At first I was wondering what could you possibly talk about 30 mins for just the function signature but I actually learned a lot. Thanks for putting this together
17
@alan1world · 1 year ago
You can specify options using Literal. From the docs: `mode: Literal["rb", "wb", "ab", "xb", "r+b", "w+b", "a+b", "x+b"],`
2
@dannorris1406 · 2 years ago
Great video as Always Arjan. Thanks for covering this topic. A real bugbear of mine is splitting function headers across multiple lines like @ <a href="https://www.youtube.com/watch?v=WI6CqykFbDc&t=494">8:14</a> this is obviously auto-formated and it's a PEP8 guideline I know but I find it makes headers much more difficult to read (unless they are really long with lots of arguments - which they shouldn't be). I started using "autopep8.args": ["--ignore=E501"] in my settings to ignore long lines.
5
@edgeeffect · 7 months ago (edited)
Oh nice!!! I try to avoid `Any` as much as possible... but "have to" use it sometimes for dictionaries... I've always wished there was a typed dictionary! And now I know that THERE IS! :)<br>(or I could just use a DataClass) ;)
1
@steveweber711 · 10 months ago
So glad to see you teach these things. It's also been my approach for many years. I love the usage of type annotations and parameter naming.
@youmal30 · 2 years ago
Regarding variable naming, I use plural form for a collection as in "cars" unless the variable name hints to a collection as in "list_car", in which case I use the singular form.
2
@antoniov845 · 2 years ago
You could use NamedTuple for Options in that case it could be destructured almost like in TS
4
Up next

How to Write Python That Feels Like Python
ArjanCodes · 174K views

Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!
ArjanCodes · 357K views

Boxing clever: stop your LLM from writing redundant code.
Chiron Codex · 176 views

Python Decorators: The Complete Guide
ArjanCodes · 198K views

Anthropic and the Riemann Hypothesis: Here’s What Actually Happened
Ellie Sleightholm · 525K views

SQL Foundation Course - Lesson 21: DML - Data Removal (DELETE and TRUNCATE)
PostgreSQLMastery · 37 views

The Sun Is Weirder Than You Think
Cleo Abram · 1.3M views

Python Properties vs Methods: The Contract You Didn’t Know You Were Making
ArjanCodes · 65K views

How to Write Great Unit Tests in Python
ArjanCodes · 50K views

I Tried SOLID Principles in Python… Here’s What Happened
ArjanCodes · 71K views

The Closest Thing We Have to Alien Technology
Veritasium · 59M views

SQLAlchemy vs SQLModel: Which Should You Use?
ArjanCodes · 130K views

EU Tells USA To BACK OFF! Dutch BAN Microsoft, Bonds COLLAPSE, Trump PANICS
House of El · 331K views

High-performance Database Architecture - An Interview with Tyler Cloutier
Molly Rocket · 124K views

The Insane Real Engineering of the Nazi Enigma Machine
Veritasium · 14M views

Learning Python Feels Easy. Until It Isn’t.
ArjanCodes · 47K views

Protocols vs ABCs in Python - When to Use Which One?
ArjanCodes · 90K views

AI Leaders Sign Trump's "Self-Policing" Pact & Eric Schmitt's Ambush Backfires | The Daily Show
The Daily Show · 2.1M views

'A febrile moment for American democracy' | Simon Marks
LBC and LBC America · 526K views