Ads skipped

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

    &quot;Make interfaces easy to use correctly and hard to use incorrectly.&quot;<br>- Scott Meyers, The Most Important Design Guideline

    83

  • @saitaro · 2 years ago

    There&apos;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 &quot;sort&quot; would be confusing in this case, it&apos;s used for the corresponding method, which actually transforms the object.<br> I don&apos;t know how this style is named though.

    14

  • @rycona-yt · 9 months ago

    In my heart, I believe &quot;kwargs&quot; 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&apos;m about to write. In that calling function, the name and arguments should read in plain English, almost like a story. Ultimately, if you&apos;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&apos;t need to dig deeper on that line to reason about what&apos;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[&quot;rb&quot;, &quot;wb&quot;, &quot;ab&quot;, &quot;xb&quot;, &quot;r+b&quot;, &quot;w+b&quot;, &quot;a+b&quot;, &quot;x+b&quot;],`

    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&amp;t=494">8:14</a> this is obviously auto-formated and it&apos;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&apos;t be). I started using &quot;autopep8.args&quot;: [&quot;--ignore=E501&quot;] 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 &quot;have to&quot; use it sometimes for dictionaries... I&apos;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&apos;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 &quot;cars&quot; unless the variable name hints to a collection as in &quot;list_car&quot;, &nbsp;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

LIVE

How to Write Python That Feels Like Python

ArjanCodes · 174K views

LIVE

Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!

ArjanCodes · 357K views

LIVE

Boxing clever: stop your LLM from writing redundant code.

Chiron Codex · 176 views

LIVE

Python Decorators: The Complete Guide

ArjanCodes · 198K views

LIVE

Anthropic and the Riemann Hypothesis: Here’s What Actually Happened

Ellie Sleightholm · 525K views

LIVE

SQL Foundation Course - Lesson 21: DML - Data Removal (DELETE and TRUNCATE)

PostgreSQLMastery · 37 views

LIVE

The Sun Is Weirder Than You Think

Cleo Abram · 1.3M views

LIVE

Python Properties vs Methods: The Contract You Didn’t Know You Were Making

ArjanCodes · 65K views

LIVE

How to Write Great Unit Tests in Python

ArjanCodes · 50K views

LIVE

I Tried SOLID Principles in Python… Here’s What Happened

ArjanCodes · 71K views

LIVE

The Closest Thing We Have to Alien Technology

Veritasium · 59M views

LIVE

SQLAlchemy vs SQLModel: Which Should You Use?

ArjanCodes · 130K views

LIVE

EU Tells USA To BACK OFF! Dutch BAN Microsoft, Bonds COLLAPSE, Trump PANICS

House of El · 331K views

LIVE

High-performance Database Architecture - An Interview with Tyler Cloutier

Molly Rocket · 124K views

LIVE

The Insane Real Engineering of the Nazi Enigma Machine

Veritasium · 14M views

LIVE

Learning Python Feels Easy. Until It Isn’t.

ArjanCodes · 47K views

LIVE

Protocols vs ABCs in Python - When to Use Which One?

ArjanCodes · 90K views

LIVE

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

The Daily Show · 2.1M views

LIVE

'A febrile moment for American democracy' | Simon Marks

LBC and LBC America · 526K views

YouTube, with the door locked.

Aegis plays a clean stream instead of YouTube's player, so pre-roll ads, trackers, and fingerprinting never ride along. Drop Shields any time if you want the official player back.