Pass a list or a tuple to a function

Documentation

  1. Unpacking Argument Lists in the Python Tutorial
  2. iterable unpacking in Expression lists in the Python Language Reference
  3. dictionary unpacking in Dictionary displays in the Python Language Reference
  4. Unpack an iterable or a mapping in Calls in the Python Language Reference

passlist.py

threeArgs: Moe Larry Curly
oneArg:    Moe Larry Curly
threeArgs: Moe Larry Curly

Things to try

  1. Does it still work if you change the list in line 23 to a tuple? Simply change the [square brackets] in line 23 to (parentheses).
  2. Instead of verifying in line 18 that the argument arg0 is a list or tuple, verify that arg0 belongs to some type that is iterable.
        try:
            _ = iter(arg0)
        except TypeError as error:
            print(f"Sorry, {arg0} of type {type(arg0)} is not iterable.", file = sys.stderr)
            return   #instead of sys.exit(1)
    
    Then update the docstring in line 17.
  3. What goes wrong if you omit the asterisk in line 25? For the asterisk, see “If the syntax *expression appears in the function call” in Calls.
    threeArgs: Moe Larry Curly
    oneArg: Moe Larry Curly
    Traceback (most recent call last):
      File "/Users/myname/python/Untitled.py", line 25, in <module>
        threeArgs(stooges)
    TypeError: threeArgs() missing 2 required positional arguments: 'arg1' and 'arg2'
    
  4. We saw this asterisk in line 172 of tkavenue.py in Manhattan, in the star example in Tkflag, and the Old MacDonald example in Tic-tac-toe.