Saturday, July 4, 2020

Mixing Positional & Keyword Arguments


We can mix both fashions if we want - there is only one unbreakable rule: we have to put positional arguments before keyword arguments.

Let’s consider below snippet.






We can get the output as





This is a pure example of positional argument passing. Also we can replace these arguments like below way.





So the output will be





Let’s try to mix both styles now.






Output will be





Let's analyze it:
  • The argument (3) for the “a” parameter is passed using the positional way.
  • The arguments for c” and “b” are specified as keyword ones.

But if we try to pass more than one value to one argument, all we'll get is a runtime error. Look at the snippet below.







This will get the following error.






Look at the snippet below. This code is fully correct, but it doesn’t make much sense.






Output is





Everything is correct. But leaving in just one keyword argument looks a bit weird.

No comments:

Post a Comment