-2

enter image description here

Kindly Explain this code that is highlighted, especially the part where the method Definition is. Questions:

  1. why Asterisk is on the Start without any parameter, What does it mean in python.
  2. Asterisk before calling a function "choice", what does it mean?
  3. How does it enforce python to use keyword-only arguments?
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Those are two completely different uses of `*`. The first defines keyword-only parameters; the second unpacks a sequence. – chepner Sep 17 '21 at 15:29

1 Answers1

1

I feel like the image explains it qzite clearly, but here is a simplified version:

* ends the positional arguments. In python positional args are always before keyword args.

def test(a, v, b):
  print(v)

test(3, v=7, 5) # illegal, kwargs have to be last (SyntaxError) 
test(3, v=7, b=5) # legal
test(3, 7, 5) # legal

But lets say we want the user to always specify v and b while leaving a positional.

def test(a, *, v, b):
  print(v)

test(3, v=7, b=5) # legal
test(3, 7, 5) # illegal, takes one positional argument but 3 were given
test(3, 5, v=7, b=5) # illegal, 2 were given
DownloadPizza
  • 3,307
  • 1
  • 12
  • 27