Positional-only and keyword-only arguments in Python

preview_player
Показать описание
Make function args positional or keyword-only.

In Python, it's possible to force a function argument to be positional-only or keyword-only. In this video, we see the syntax for doing this, as well as see some examples and reasons for why you might want to actually make a parameter positional-only or keyword-only.

Note: positional-only arguments are a Python 3.8+ feature. Keyword-only are Python 3.0+.

Errata:
1. At 1:22 you'll get a TypeError, not a SyntaxError.

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, John Martin, Casey G

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
1:08 Keyword-only arguments
4:50 Positional-only arguments
7:45 Uncommon to use both
8:29 Speed test, position args are faster
Рекомендации по теме
Комментарии
Автор

Every video you drop is so hugely important. Youre single handedly saving my in-production code from ignorance

ethanevans
Автор

Wow. On other channels this video would just have been the display of the syntax and some code examples. But as always, you share valuable knowledge in the form of use cases, benchmarking, 'tricks' to remember the syntax, etc. I yearn to one day be as good of an educator as you. Thanks for the video!

matheusaugustodasilvasanto
Автор

What I love about these videos is that each highlights one specific topic that you rarely see anywhere else, yet all of them have real world applications. Thank you James

vladyslavkotov
Автор

That explanation was CLEAN! And the speed test was the icing on the cake! Thank you so much

dsenese
Автор

When the positional only stuff came out, I also remember hearing that there were some reasons related to the C foreign function interface. Most other languages don’t have kwargs, and this (supposedly) helped maintain symmetry when you were defining wrappers for C functions

kyle-silver
Автор

Another engaging and informative video with a pleasantly narrow scope 🤠

aaronm
Автор

One use-case I've heard for positional-only args is that it allows you to have a truly-general kwargs parameter.

Like, say you had a function that lets you modify some sort of general-purpose configuration settings, and it needs to take two things as input: a section to put the settings in, and some key-value pairs for what settings to change. The definition could look like:
def modify_config(section, **changes):
...
and you'd then call this like:
modify_config("subscriptions", mCoding=True)

But what if you wanted to have a setting that is literally called "section"? You can't call:
modify_coding("a thing", section="left")
because it will treat the "section=" parameter as being the first arg to the function, not a part of the kwargs, and you'll get an error that you're providing two values for the same argument.
So our function isn't completely general-purpose, we can't use it to change a setting that's called "section".

However, what if we change that parameter to be positional-only?
def modify_config(section, /, **changes):
...
modify_config("a thing", section="left")
Now, the call works properly, exactly as we'd expect it, and the section="left" parameter ends up in the kwargs.

mrphlip
Автор

I recommend your videos all the time. The depth on a narrow topic is exactly the type of video I've been after lately. You dont just explain what it does and some examples, you show exactly how something works and you somehow do it concisely.

stevenluoma
Автор

This is the most clear and concise explanation and how-to tutorial on python arguments I have seen. Excellent job. Bravo!

wilcosec
Автор

Great video. I recently came across your videos and have been gobbling them up and passing them along to my coworkers. Thank you so much for doing this and I really appreciate the nicely digestible length of your videos yum yum.

rhtcguru
Автор

You always provide a new (deeper) perspective on seemingly simple concepts. Im glad I found your channel, you have the best content Ive seen on YT.

VojtechMach
Автор

one of the best explanation (about args and kwargs) i've ever heard

mostafaseyedashor
Автор

Very good video! I ran your script with pypy 3.10, and the timings were practically identical between the different passing techniques.

supersteve
Автор

Thank you so much. I've been wondering about the slash in function parameters for so long

knut-olaihelgesen
Автор

Damn, man these videos are what I wanted, one the edge python cases/implementation or advanced video.

georgedicu
Автор

Had never known about this syntax, but I can think of some good usecases allready! Your video's are really the best for learning these obscure but very often usefull features you don't see in beginners courses.

CodingDragon
Автор

I learned an additional thing from this, as I didn't know about the {var_name=} feature of f-strings, so thank you for that as well!

Mx_Flix
Автор

I learned something new today. Thanks for making this video!

POINTS
Автор

Banging video. Learnt something new, seems advanced but simple to understand and use. Love it

aliwelchoo
Автор

This has been in python since 3.0 and I never knew what it meant (have not seen it in many codebases with the exception of boto3 stubs) thanks for this!

fallingintime
join shbcf.ru