-1

I want to pass a variable number of arguments to a function, the first argument represents a command, and the remaining arguments represent the arguments to the command. For example:

def do(*argv):
    command = argv[0]

    match command:
        case "doNothing":
            print("do nothing")
        case "create":
            arg = argv[1]
            print(arg)
        case "add":
            arg1 = argv[1]
            arg2 = argv[2]
            print(arg1 + arg2)
        case default:
            return "something wrong with the input to do()"

do("create", [1,2])

and run it

$ python ./my.py
  File "/home/t/my.py", line 8
    match command:
          ^
SyntaxError: invalid syntax

How can I parse the variable number of arguments?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tim
  • 1
  • 141
  • 372
  • 590
  • I think you meant to do `command = argv[0:]` but then why slice the `argv` if you are going to take all arrays anyways. – Bijay Regmi Apr 22 '22 at 00:04
  • Use the `head, *tail` pattern of unpacking iterables: `cmd, *args = argv` – ddejohn Apr 22 '22 at 00:05
  • 2
    If you're getting a syntax error with `match command` or `match argv[0]` then you don't have Python 3.10. – ddejohn Apr 22 '22 at 00:13
  • @ddejohn could you elaborate both comments? How can I get argv[1]? Why is it not? – Tim Apr 22 '22 at 00:13
  • @Tim it's not clear what you're trying to do. Are you trying to split the args and the command? Then do `cmd, *args = argv` and proceed with your logic. If you're trying to solve a syntax error, then you likely aren't using the correct version of Python for structural pattern matching. – ddejohn Apr 22 '22 at 00:14
  • @ddejohn. correct, mine is 3.9. What can I use instead? – Tim Apr 22 '22 at 00:14
  • https://docs.python.org/3/whatsnew/3.10.html – ddejohn Apr 22 '22 at 00:14
  • 1
    I recommend looking at the [official tutorial](https://peps.python.org/pep-0636/), as well. This is a brand new feature in Python. – ddejohn Apr 22 '22 at 00:16

1 Answers1

-3

for Python >= 3.10:
there is match - case support. see example in documentation

msg = 2
match msg:
    case 0:
        print('no')
    case 1:
        print('no')
    case 2:
        print('yes')

#output:==> "yes"


for Python < 3.10:
it does NOT support "swich - case" like Javascript

match ____:
    case ___:
      ....

instead, use if - elif - else
try this:

def do(*argv):
    command = argv[0]
    if command == "doNothing":
        print("do nothing")
    elif command == "create":
        arg = argv[1]
        print(arg)
    elif command == "add":
        arg1 = argv[1]
        arg2 = argv[2]
        print(arg1 + arg2)
    else:
        return "something wrong with the input to do()"

do("create", [1,2])
# argv is ["create", [1,2]]
# so, 
#       arg = argv[1]
#        print(arg)
#
# will print [1, 2]
ibrahem
  • 370
  • 3
  • 12
  • It does in 3.10 and later. – CrazyChucky May 13 '22 at 16:53
  • @CrazyChucky, no it doesn't . not like javascript. I think you mean implementing it with a dict like this https://www.upgrad.com/blog/how-to-implement-switch-case-functions-in-python/ – ibrahem May 13 '22 at 17:00
  • If I am wrong, please give me a working example – ibrahem May 13 '22 at 17:00
  • It is not exactly like in JavaScript, but it was definitely added in 3.10. https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching – CrazyChucky May 13 '22 at 17:04
  • To clarify: while it can do more than JavaScript's switch/case, it can also be used to accomplish the same thing. See [the simple example](https://docs.python.org/3/whatsnew/3.10.html#simple-pattern-match-to-a-literal) in the linked release notes. – CrazyChucky May 13 '22 at 17:32
  • thanks! I haven't known about this feature as I was using Python 3.9 – ibrahem May 13 '22 at 17:44