Positional-only and keyword-only arguments in Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 191

  • @ethanevans8909
    @ethanevans8909 2 ปีที่แล้ว +282

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

    • @mCoding
      @mCoding  2 ปีที่แล้ว +199

      I take full credit! Unless anything goes wrong, then you did it all on your own!

    • @cobalius
      @cobalius 2 ปีที่แล้ว +2

      Yes! I think the same about it!

    • @enisten
      @enisten 2 ปีที่แล้ว +17

      @@mCoding Sounds like my boss.

    • @markcuello5
      @markcuello5 2 ปีที่แล้ว

      Thanks; and I`m glad You`re happy.

    • @Y2B123
      @Y2B123 2 ปีที่แล้ว +1

      @@mCoding I think Trump stole your quote lol.

  • @matheusaugustodasilvasanto3171
    @matheusaugustodasilvasanto3171 2 ปีที่แล้ว +77

    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!

  • @vladyslavkotov7570
    @vladyslavkotov7570 2 ปีที่แล้ว +20

    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

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      Thanks for the support!

  • @dsenese
    @dsenese 2 ปีที่แล้ว +25

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

    • @mCoding
      @mCoding  2 ปีที่แล้ว +2

      Glad you enjoyed! I hope you find many other desserts on my channel.

  • @mrphlip
    @mrphlip 2 ปีที่แล้ว +60

    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.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว +1

      While your example works, I can’t help feeling it’s a little strained.

    • @shreyashchauhan6359
      @shreyashchauhan6359 2 ปีที่แล้ว +4

      @@lawrencedoliveiro9104 strained how if may ask?

    • @leogama3422
      @leogama3422 2 ปีที่แล้ว +1

      This is exactly what the dict() function does.(Even before the feature was added to the language, it was implemented as a special case in C)

    • @JorgeLuis-ts6qp
      @JorgeLuis-ts6qp 2 ปีที่แล้ว

      In fact this is how Pablo Galindo Salgado justified the modification. The other option, let your function swallow everything, would have imply a horrible documentation. In addition this feature was already in use in the insights of the standard library. It just has become available for the Python users.
      As a fun fact the usage of the bar let the standard library implementation to be reduced in 3,000 lines.

  • @kyle-silver
    @kyle-silver 2 ปีที่แล้ว +41

    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

    • @mCoding
      @mCoding  2 ปีที่แล้ว +18

      That would make sense, the default for wrapping a function in a language that doesn't have kwargs makes sense to be positional only.

    • @d00dEEE
      @d00dEEE 2 ปีที่แล้ว +5

      Yeah, there have been positional-only built-ins forever, so when Argument Clinic was implemented, it supported them and the use of "/" (the "inverse" of "*") was adopted there. Then people started asking if they could write pure-Python functions with the same APIs as those in the built-ins, and the "/" was added to Python itself.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว +2

      That doesn’t make any sense. Keyword arguments allow Python to rearrange the arguments into the right order to pass to a lower-level function. The fact that the lower-level function is written in a language that doesn’t support keyword arguments is irrelevant, and no excuse for the higher-level language to limit itself.

    • @d00dEEE
      @d00dEEE 2 ปีที่แล้ว

      @@lawrencedoliveiro9104 Well, Python didn't always have keyword arguments (I can't recall, but maybe added in 1.4 or 1.5?). At the time, creating keyword argument lists in the interpreter was quite expensive, so the already-existing builtins were left as positional-only, and that has been carried through history.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว

      @@d00dEEE Many existing functions have had keyword arguments added. By its nature, it doesn’t break backward compatibility.

  • @aaronm6675
    @aaronm6675 2 ปีที่แล้ว +41

    Another engaging and informative video with a pleasantly narrow scope 🤠

    • @mCoding
      @mCoding  2 ปีที่แล้ว +2

      Glad you enjoyed it!

  • @wilcosec
    @wilcosec 2 ปีที่แล้ว +2

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

  • @beto5720
    @beto5720 2 ปีที่แล้ว

    Times I refactor my code before finding your channel = once, times I refactor my code after finding your channel = once a day

  • @fallingintime
    @fallingintime 2 ปีที่แล้ว +2

    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!

  • @christoferberruzchungata2722
    @christoferberruzchungata2722 2 ปีที่แล้ว +1

    I learned this from the book “90 Specific Ways to Write Better Python”. It’s such a great book that I would recommend to anyone looking to become a more professional Python developer

  • @supersteve1440
    @supersteve1440 4 หลายเดือนก่อน

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

  • @rhtcguru
    @rhtcguru 2 ปีที่แล้ว +1

    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.

    • @mCoding
      @mCoding  2 ปีที่แล้ว

      Haha gobble gobble. Enjoy! Glad you like my videos.

  • @Mx_Flix
    @Mx_Flix 2 ปีที่แล้ว +1

    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!

  • @mostafaseyedashor8768
    @mostafaseyedashor8768 2 ปีที่แล้ว

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

  • @sharpfang
    @sharpfang 2 ปีที่แล้ว

    I worked with a (proprietary) PHP framework which had an approach of making as few args as possible mandatory, and as many parameters of an operation args with sensible defaults. The "keyword arguments" was the way it was implemented - you'd often have a function with 15 args, one required, the rest - as needed. It was very friendly in use that way - bare minimum to get it to work, easy to change whatever you want changed.

  • @slava6105
    @slava6105 2 ปีที่แล้ว +3

    Note that int.__pow__ (and maybe other number types) has "mod" parameter especially for cases "(x ** y) % mod"

    • @mCoding
      @mCoding  2 ปีที่แล้ว

      Good catch! Thanks for pointing this out.

  • @justingerber9531
    @justingerber9531 2 ปีที่แล้ว +4

    Awesome video! I like the def foo(*, arg_1, arg_2, arg_3=default_3, ...): pattern to force users to explicitly pass in keyword names. This is useful for methods that have a lot of inputs to a) force users to look up the signature so they see everything that they need to pass and are able to pass in and b) ensure no parameter mix ups occur. This approach also makes code a bit more explicit which can be helpful. Of course it's more verbose which could be seen as a downside.
    It would be nice to see examples where you include default arguments as well. When I first learned about these I was confused by the fact that there was a difference between keyword only args and optional args (before I erroneously thought they were one and the same). I also think there's a confusing case of: Can I have position-only args with and without defaults while also having keyword-only args with and without defaults?
    I don't really need to see examples where there are explicitly positional or keyword args because I feel like those cases are just confusing, and like you helpfully pointed out, not really useful.
    I do have to say that I've hesitated to put these patterns in much of my code though, because I believe it's python 3.9+ and at the time I learned it I was still working with lower versions of python. I also hesitate to put it in because I think most people don't know what it means and it would surprise them.

    • @leogama3422
      @leogama3422 2 ปีที่แล้ว

      The positional-only feature is recent, but the "/" syntax has been used in the built-in functions' documentation for a long time

  • @VojtechMach
    @VojtechMach 2 ปีที่แล้ว

    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.

    • @mCoding
      @mCoding  2 ปีที่แล้ว

      Great to hear, your views are appreciated!

  • @CodingDragon04
    @CodingDragon04 2 ปีที่แล้ว +10

    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.

  • @mohammadrezakarimi8180
    @mohammadrezakarimi8180 2 ปีที่แล้ว +1

    Loved the "yum"! Also, James, where is your face? It was so nice to see you narrating the video :)
    Great work. Really appreciate your efforts.

  • @knut-olaihelgesen3608
    @knut-olaihelgesen3608 2 ปีที่แล้ว +1

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

  • @joseville
    @joseville 2 ปีที่แล้ว

    5:05 best part!
    0:40 jfyi for the completionist
    foo(2, a=1, c=3) is a TypeError
    foo() got multiple values for argument 'a'

  • @cafecafeliebe
    @cafecafeliebe ปีที่แล้ว

    Star is like star-args but without the args 🤣 mCoding is like m but with Coding 😄 Your content is so advanced, I love it! 👌

  • @georgedicu7397
    @georgedicu7397 2 ปีที่แล้ว

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

  • @mihai-gabriel-07
    @mihai-gabriel-07 2 ปีที่แล้ว +1

    Amazing video. I've actually never seen this feature used before but I feel like it should be best practice, especially when writing APIs

  • @cyber-dioxide
    @cyber-dioxide 2 ปีที่แล้ว

    Your channel booted up my python skills x10 🥂

  • @parsasalavati1006
    @parsasalavati1006 2 ปีที่แล้ว +5

    Great videos! ✌
    Could you do an in-depth video on asyncio?
    By in-depth I mean how it works under the hood; Particularly how it compares to & differs from multithreading, how does python know when to "wait for this to finish and run some other code in the meantime" and what happens under the hood when the results are ready and how it relates to OS scheduling and sleep queue.
    So basically, content that you find on this channel that it's harder to find elsewhere.

    • @mCoding
      @mCoding  2 ปีที่แล้ว +4

      This has been on my list for a long time!

    • @PanduPoluan
      @PanduPoluan ปีที่แล้ว

      Lukasz Langa had made a series of videos if you really want a deep dive into asyncio: th-cam.com/play/PLhNSoGM2ik6SIkVGXWBwerucXjgP1rHmB.html

  • @sovietmaths5651
    @sovietmaths5651 2 ปีที่แล้ว +1

    wow, just started learning about this. You're amazing, James.

    • @mCoding
      @mCoding  2 ปีที่แล้ว

      Very welcome and thanks for watching!

  • @Ganerrr
    @Ganerrr 2 ปีที่แล้ว +42

    I somehow have never see this syntax. I've seen mentions of this while manipulating python ASTs but I didn't know what they really meant lol

    • @Ganerrr
      @Ganerrr 2 ปีที่แล้ว +17

      @@mrdkaaa you read the docs? nerd

    • @pranavnyavanandi9710
      @pranavnyavanandi9710 2 ปีที่แล้ว

      What does ASTs mean?

    • @Ganerrr
      @Ganerrr 2 ปีที่แล้ว +1

      @@pranavnyavanandi9710 abstract syntax tree, basically I was modding python to make creating and manipulating lambdas easier lol

  • @POINTS2
    @POINTS2 2 ปีที่แล้ว +2

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

  • @Yotanido
    @Yotanido 2 ปีที่แล้ว

    I've defined keyword-only parameters before, but I honestly had no idea positional-only existed. Doubt I'll find much use for it, but good to know.

  • @JoQeZzZ
    @JoQeZzZ ปีที่แล้ว

    I never knew about plain *, it makes total sense.

  • @diegomountain7177
    @diegomountain7177 2 ปีที่แล้ว

    This was super informative. I understand a little better how to use *args and **kwargs which I know wasn't the point of this video. But still, this was very useful. Thank you!

  • @grunklestanlee2774
    @grunklestanlee2774 2 ปีที่แล้ว

    I’ve actually used this before without really knowing what it was doing while making a discord bot

  • @RedBearAK
    @RedBearAK ปีที่แล้ว

    OMG, I had to have a “dummy” parameter as the first argument because I couldn’t find a simple way to force all parameters to be named. Never found this syntax of just star-comma offered as a solution. It’s quite logical to remember that it’s like “*args” without the argument name. As if it just doesn’t give anywhere for the positional args to go.

  • @aliwelchoo
    @aliwelchoo 2 ปีที่แล้ว +1

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

  • @xan1716
    @xan1716 2 ปีที่แล้ว +1

    This I find helpful when there're a lot of boolean args, which if passed without keywords, would make its invocation harder to understand -- e.g. `process_two_numbers(3, 5, True, True, False, None, True)` -- no idea what's happening here without forced kwargs :)

  • @n49o7
    @n49o7 2 ปีที่แล้ว +1

    My takeaway:
    star yum
    star star yum yum
    slash NO NAME FOR YOU
    Great vid !

  • @hamzasayyid8152
    @hamzasayyid8152 2 ปีที่แล้ว +1

    Never seen this before. So cool. I wonder why positional only is faster than keyword. Thought it would be backwards

    • @lunarmagpie4305
      @lunarmagpie4305 2 ปีที่แล้ว

      I think its because keyword arguments are stored as a dict and positional arguments are a tuple. A dict is a considerably more expensive data structure.

  • @JoseHenrique-xg1lp
    @JoseHenrique-xg1lp 2 ปีที่แล้ว

    The benchmark at the end was the icing on the cake. Is there a performance change? Not enough to bother, but at least we know that

  • @PhoenixClank
    @PhoenixClank 2 ปีที่แล้ว

    Until now I haven't used this feature a lot because I don't want to put restrictions on the users of my code. However, I make most boolean arguments keyword-only, because when passed as a literal it's usually not clear from reading the code what the argument is supposed to mean.

  • @SkrtlIl
    @SkrtlIl 2 ปีที่แล้ว +3

    Is power mod slow because you could also implement binary exponentiation together with taking the mod on the output on each iteration?

    • @mCoding
      @mCoding  2 ปีที่แล้ว +5

      Yes exactly, fast pow and take mod n each iteration will be much much faster then computing a huge exponentiation then reducing mod n.

  • @Talon_24
    @Talon_24 2 ปีที่แล้ว +1

    My usual examples for why you would want keyword/positional-only arguments are:
    Keyword Only: consider n_sorted(data, true) -- what does the 'true' set there? ascending/descending? nulls first/last? Maybe something else? What if you would find n_sorted(data, false, true) -- which true and false is which? is the first false only there to keep the second argument as default to reach the third positional argument? n_sorted(data, nulls_last=True) is unambiguous about that.
    Positional only: Reason to enforce positional order could be if the ability to change the order would only exist to mess with the person reading the code. Similar to your example, divide(y=2, x=3) or Fraction(denominator=2, numerator=3) would seem to be only there to obfuscate that this is 3/2 and not 2/3

    • @KappakIaus
      @KappakIaus 2 ปีที่แล้ว

      But why not just leave the caller the option? In the first case, they can still use keyword arguments if they feel like the ambiguity might be a problem. Same thing in the 2nd example. If the caller decides to swap the argument order, they probably had a good reason for it.
      Basically, you are trying to force the person using your code to do what you think is best for them. But by doing so, you are blocking off use-cases you might not have thought about.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว

      Keyword-only arguments can be useful where you think you might add more later, and maybe change the order.
      There is one situation where there is a specific use for them: normally once you specify a default value for a formal argument, all remaining ones must also have defaults. But in the keyword-only section, this rule no longer applies, and you can omit the default for an arg that comes after one that has a default.

  • @Khushpich
    @Khushpich 2 ปีที่แล้ว +1

    Thanks James, great video. In what python version of python was this added?

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 ปีที่แล้ว

      Keyword-only arguments were introduced in 3.0.

    • @sadhlife
      @sadhlife 2 ปีที่แล้ว

      positional only was 3.8

  • @Epinardscaramel
    @Epinardscaramel 2 ปีที่แล้ว

    Oh that looks great! I have to use it in the library I'm writing

  • @RemotHuman
    @RemotHuman 2 ปีที่แล้ว

    The best part of this is f"{x=}" I didnt know you could do that

  • @tytywuu
    @tytywuu 2 ปีที่แล้ว +1

    I think I finally remember the syntax... the / * is really hard to remember

  • @deeplearnit
    @deeplearnit 2 ปีที่แล้ว

    I always learn something new from your videos, thank you!

  • @turbine1
    @turbine1 2 ปีที่แล้ว

    So good. Never heard about it the pure * before and directly implemented it in my code

  • @JonDoe-oy8bt
    @JonDoe-oy8bt 2 ปีที่แล้ว

    4:58. If you want to swallow up keyword args you can just use **kwargs. This isn't exactly a parallel to / but is the equivalent kwargs version of *args

  • @arbaazshafiq
    @arbaazshafiq 2 ปีที่แล้ว +4

    Congrats on the 100k

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      Thank you so much 😀

  • @ayyythatguy
    @ayyythatguy 2 ปีที่แล้ว

    Such a useful video I wondered how to do this exact case yesterday by chance!!

  • @sadhlife
    @sadhlife 2 ปีที่แล้ว

    4:00 this is also why most projects should probably use mypy and type annotations :)

  • @XcaliburKurosaki
    @XcaliburKurosaki 2 ปีที่แล้ว

    "We don't need x."
    I feel attacked lmao
    Jokes aside, this has been an awesome watch with some immediate gains in my codebases 🥰

  • @diamonddemon7612
    @diamonddemon7612 2 ปีที่แล้ว

    This is EXACTLY what I need today!

  • @maartenofbelgium
    @maartenofbelgium 2 ปีที่แล้ว +2

    4:35 Buying 10,000 items of $4,500 each, costs as much as buying 4,500 items of $10,000 each.
    Unless you're the seller :)

    • @mCoding
      @mCoding  2 ปีที่แล้ว +3

      I think you'll find the buyer would be much happier to receive 10000 units rather than 4500 units for the same price! (Assuming there aren't any crazy storage costs, minimum holding times, or other penalties incurred for holding units).

    • @maartenofbelgium
      @maartenofbelgium 2 ปีที่แล้ว +2

      @@mCoding These are no bugs, just happy mistakes.

  • @PhyFlame
    @PhyFlame 2 ปีที่แล้ว +1

    I never knew about the '/' and '*' in the function parameter list - they are awesome.
    Also, I realised that I can cheat the "slap the like button an odd number of times" system by hitting it twice, but also clicking dislike once in between. Under the cost of the emotional baring of having your video disliked for a moment this still results in a like ;)

  • @XCanG
    @XCanG 2 ปีที่แล้ว

    7:22 Your note about slow function, what would be faster equivalent?

    • @mCoding
      @mCoding  2 ปีที่แล้ว

      In the algorithm to compute pow, which is typically some kind of repeated squaring algorithm, take mod n each step instead of at the end. The builtin pow function also takes a mod parametee that does this.

  • @Hyrtsi
    @Hyrtsi 2 ปีที่แล้ว

    I've always wondered what **kwargs means but never bothered to look up. Thanks!

  • @hupa1a
    @hupa1a 2 ปีที่แล้ว

    Very interesting insights
    Thank you for that video!

  • @codingcrashkurse6429
    @codingcrashkurse6429 2 ปีที่แล้ว

    Saw this some weeks ago in a colleagues Code.

  • @MrFibbanacci
    @MrFibbanacci ปีที่แล้ว

    U can memorize /* which start multiline comment in if I remember correct.
    You are welcome! : )

  • @ali-om4uv
    @ali-om4uv 2 ปีที่แล้ว

    I have rhe impression you are probably resonably decent at writing python code :-)

  • @arisweedler4703
    @arisweedler4703 2 ปีที่แล้ว

    I love it. I want to feed the YT algorithm so I make this comment.

  • @glass-ships
    @glass-ships 2 ปีที่แล้ว

    This isn't really important but..
    10,000x4500 and 4500x10,000 are the same since multiplication is commutative.
    Awesome video as always though!

  • @fartzy
    @fartzy 2 ปีที่แล้ว

    Amazing stuff as always my good man

  • @denissetiawan3645
    @denissetiawan3645 2 ปีที่แล้ว

    superb explanation

  • @quintencabo
    @quintencabo 2 ปีที่แล้ว

    You should be able to put the star next to the slash if you don't need the middle

  • @Xavier-es4gi
    @Xavier-es4gi 2 ปีที่แล้ว

    This is not related to the video but since there are probably many python expert here I'll ask my question. Can I have some tips/good practices about how to create and organize a python package. Also what should be favored, relative or absolute imports? I prefer absolute because theye are easy to copy paste between files.

  • @merthyr1831
    @merthyr1831 2 ปีที่แล้ว

    Being a dart developer where the convention is to use keyword arguments over positional ones, I wish more languages used that convention too! Makes reading others code a LOT easier when you don't have to learn the order of arguments in a function. Dart has extra features to make keyword args easier to use too (such as automatically filling backed fields) but definitely a feature you should use in other languages too!!

  • @robertbrummayer4908
    @robertbrummayer4908 2 ปีที่แล้ว

    Great and interesting video

  • @suvajitmitra58
    @suvajitmitra58 ปีที่แล้ว

    great explanation! thanks!!

  • @bettercalldelta
    @bettercalldelta 2 ปีที่แล้ว

    7:43 I actually recently used that in my project lol

  • @AsgerJon
    @AsgerJon 2 ปีที่แล้ว

    What is the "good practice" way of implementing keyword arguments with default values?

    • @ДмитроПрищепа-д3я
      @ДмитроПрищепа-д3я 2 ปีที่แล้ว

      something like
      def f(x=None):
      if x is None:
      x = smth
      That is unless you also plan to allow for None as a valid value, then it's a bit more complicated

    • @AsgerJon
      @AsgerJon 2 ปีที่แล้ว

      @@ДмитроПрищепа-д3я So it's actually the same as for a positional argument? What I am doing now is to have a default dictionary, I loop through its keys, if the key is in kwargs.keys(), I replace the default value with kwargs[key]. This feels... too javascripty, i don't know.
      The reason I prefer something like this that I would like to have a function taking an arbitrary kwargs dictionary with an arbitrary default value dictionary.

    • @ДмитроПрищепа-д3я
      @ДмитроПрищепа-д3я 2 ปีที่แล้ว

      @@AsgerJon oh, in case of multiple defaults in a dictionary like that, I think it's a fine approach. I code in TS/JS a lot, so I don't see any real problem there)
      Another way to do it is to use an update method of a dictionary and pass kwargs in it.
      You'll have something like:
      def f (**kwargs):
      kvals={"x": 1, "y": 2}
      kvals.update(kwargs)
      This will essentially merge the two dicts with overwrites, so if some key was only in kvals, it'll stay there with default value. If it's in both dicts, the value will come from kwargs.
      And I think it might be faster than looping by hand, but I never measured the performance.

    • @AsgerJon
      @AsgerJon 2 ปีที่แล้ว

      ​@@ДмитроПрищепа-д3я Speed is no concern ever in Python. If part of your code for real causes speed issues, then numba.jit and possibly even cuda jit.
      Here's what I'll do: I will subclass the default dictionary, into one that does as you suggest, but then I will put it in the __add__ method. Like:
      self.defVals = {...}
      self.defVals += kwargs
      BOOM! Python all the way!

  • @MrSteini124
    @MrSteini124 2 ปีที่แล้ว

    Wonderful! Great as always

  • @walkdead94
    @walkdead94 2 ปีที่แล้ว

    I always wanted to know whataheck that * meant on the others code... dam.. thank you.. that was greatly useful!

  • @bzboii
    @bzboii 2 ปีที่แล้ว

    How does this work w partial application?

  • @ThankYouESM
    @ThankYouESM 2 ปีที่แล้ว

    I now very much want to know how to make a Python3-Thinter-to-HTML5 app far as it can get... because I very much struggle many thousands of times more with every other programming language despite the 6 years I tried. Granted... p5js has recently got my attention, but... it still looks a bit overly complicated to re-edit which I accomplished very much the same graphics which is too slow and doesn't transfer to Android via fully* the SD card. Unfortunately... I can't even find an entire side-by-side syntax comparison.

  • @MCKBURNHOUSE
    @MCKBURNHOUSE 2 ปีที่แล้ว

    This is GOLD!

  • @matt566
    @matt566 2 ปีที่แล้ว

    Wouldn’t it make sense from an enterprise standpoint that all arguments are passed as keywords? Otherwise the reader has to reference the functions implementation or comments if it has them to understand what the parameters mean.

  • @RobbieHatley
    @RobbieHatley 2 ปีที่แล้ว

    Interesting, but you should point-out that not only must positional arguments be given _first,_ but they must be given starting at position 1 and going upward contiguously. You don't mention that. I tried calling a function like this: "print(Fred(2.1,a=1.43,c=-0.37))", but I got a "multiple values for argument 'a'" error. My assumption was that the Python interpreter is smart enough to assume that any arguments for which parameters are not supplied are intended for the remaining parameters. _No, it is not._ Instead, the first positional argument will go to first parameter, second to second, etc. In other words, the positionality is triply absolute: no offsets, skip-overs, or loop-backs are allowed.

  • @Techiesse
    @Techiesse 2 ปีที่แล้ว

    Awesome! Props to you.

  • @markcuello5
    @markcuello5 2 ปีที่แล้ว

    `mCoding` seems like A/M`tion; similar to `Playwright`.

  • @崔昆俞
    @崔昆俞 2 ปีที่แล้ว

    Very helpful !😀

  • @joffreybluthe7906
    @joffreybluthe7906 2 ปีที่แล้ว

    Great content once more!

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 ปีที่แล้ว

    6:24 How about “truthvalue” or “flag”. Or even “colbert”.

  • @紺野-純子
    @紺野-純子 ปีที่แล้ว

    you love to see it

  • @jullien191
    @jullien191 2 ปีที่แล้ว

    Il y a grep sur Windows?

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      WSL

  • @collinahn
    @collinahn 2 ปีที่แล้ว

    thanks for the video

  • @trag1czny
    @trag1czny 2 ปีที่แล้ว +4

    great vid as always
    btw discord gang 🤙🤙

    • @jacanchaplais8083
      @jacanchaplais8083 2 ปีที่แล้ว

      the video is 9 minutes long and only uploaded 2 minutes ago 🤔

    • @user-pl6hc4kj1o
      @user-pl6hc4kj1o 2 ปีที่แล้ว

      4x speed?

    • @trag1czny
      @trag1czny 2 ปีที่แล้ว +1

      @@jacanchaplais8083 I had access to it before publication as a moderator

    • @jacanchaplais8083
      @jacanchaplais8083 2 ปีที่แล้ว

      @@trag1czny fair haha

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      Thank you for your loyal viewing!

  • @salahjaabari8931
    @salahjaabari8931 2 ปีที่แล้ว +1

    I love your videos ❤️

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      Thanks so much!

  • @clementlelievre4600
    @clementlelievre4600 ปีที่แล้ว

    thanks for the good content

    • @mCoding
      @mCoding  ปีที่แล้ว

      You are very welcome, glad you enjoyed!

    • @clementlelievre4600
      @clementlelievre4600 ปีที่แล้ว

      @@mCoding in my IDE I have an extension that pre-writes docstrings for me, based on the parsing of the function body and signature, but it does not capture * and / in this context. I made a feature request ;)

  • @jenaf3760
    @jenaf3760 2 ปีที่แล้ว

    damn I envy people who can work with python.
    I work on an ancient mutated php codebase.
    I recently found some functions with a messy signsture and a bunch of option paramters.
    That kind of stuff is just bad code in php.
    In python that might work If I used the method to force those arguments to be keyword only.
    (in other places the codebase expects (what php calls) an "array" with keyword=>value pairs wich basically simulates keyword only args.
    fun times.

  • @ludo3941
    @ludo3941 2 ปีที่แล้ว

    Pretty interesting

  • @kotslike
    @kotslike 2 ปีที่แล้ว

    Amazing!

  • @QuantumHistorian
    @QuantumHistorian 2 ปีที่แล้ว +1

    Is this a recent addition to the python standard library? Have literally never seen this at all. Forcing keyword only arguments seem like it could be useful, but positional only feels really niche. I can't help but think that this is a move away from python's duck-typing, and trying to replicate the sort of checks that you get at compiler time in a statically typed language. But because this only throws an error at runtime, the gains are pretty marginal IMO. Still, good to know, even if only to read other people's code.

    • @NicolasChanCSY
      @NicolasChanCSY 2 ปีที่แล้ว +2

      Positional-only argument is added in Python 3.8 (PEP 570).
      And Keyword-only argument goes way back to Python 3.0 (PEP 3102).

  • @paulomartins1008
    @paulomartins1008 2 ปีที่แล้ว

    Im sad I didn't read this before attempting to code a classless version of Blackjack.

  • @MithicSpirit
    @MithicSpirit 2 ปีที่แล้ว +3

    Discord gang

    • @mCoding
      @mCoding  2 ปีที่แล้ว +1

      Always a pleasure!

  • @baudneo
    @baudneo 2 ปีที่แล้ว +1

    Yeah boi!