2

I met a weird case in the function definition in python, I read some code like this:

def abc(dd, *, ee=None): 
    print(dd, ee)

In the beginning, I thought this code is wrong and maybe a typo of *args, but recently I tried this code in latest python3.7, and it seems that it can be interpreted, and usage is also super wired, you can't pass more than 1 argument to this function:

>>> abc(11, 222)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    abc(11, 222)
TypeError: abc() takes 1 positional argument but 2 were given
>>> abc(11)
11 None

I am asking because I don't know the purpose of why some one wrote like this, and why python support this behaviour in Python3(not supported in python2)

khelwood
  • 55,782
  • 14
  • 81
  • 108
ZhouQuan
  • 1,037
  • 2
  • 11
  • 19

1 Answers1

0

It seems your function has 1 positional argument and one named argument. The * eats all other positional arguments, just like an *args would, but you cannot reference them.

Sebastian Ärleryd
  • 1,774
  • 14
  • 19