circuitpython/tests/basics/fun_callstar.py
David Lechner 783b1a868f py/runtime: Allow multiple *args in a function call.
This is a partial implementation of PEP 448 to allow unpacking multiple
star args in a function or method call.

This is implemented by changing the emitted bytecodes so that both
positional args and star args are stored as positional args.  A bitmap is
added to indicate if an argument at a given position is a positional
argument or a star arg.

In the generated code, this new bitmap takes the place of the old star arg.
It is stored as a small int, so this means only the first N arguments can
be star args where N is the number of bits in a small int.

The runtime is modified to interpret this new bytecode format while still
trying to perform as few memory reallocations as possible.

Signed-off-by: David Lechner <david@pybricks.com>
2022-03-31 16:59:30 +11:00

54 lines
808 B
Python

# function calls with *pos
def foo(a, b, c):
print(a, b, c)
foo(*(), 1, 2, 3)
foo(*(1,), 2, 3)
foo(*(1, 2), 3)
foo(*(1, 2, 3))
foo(1, *(2, 3))
foo(1, 2, *(3,))
foo(1, 2, 3, *())
foo(*(1,), 2, *(3,))
foo(*(1, 2), *(3,))
foo(*(1,), *(2, 3))
# Another sequence type
foo(1, 2, *[100])
# Iterator
foo(*range(3))
# pos then iterator
foo(1, *range(2, 4))
# an iterator with many elements
def foo(*rest):
print(rest)
foo(*range(10))
# method calls with *pos
class A:
def foo(self, a, b, c):
print(a, b, c)
a = A()
a.foo(*(), 1, 2, 3)
a.foo(*(1,), 2, 3)
a.foo(*(1, 2), 3)
a.foo(*(1, 2, 3))
a.foo(1, *(2, 3))
a.foo(1, 2, *(3,))
a.foo(1, 2, 3, *())
a.foo(*(1,), 2, *(3,))
a.foo(*(1, 2), *(3,))
a.foo(*(1,), *(2, 3))
# Another sequence type
a.foo(1, 2, *[100])
# Iterator
a.foo(*range(3))