From 230fec77d7bd9bae5e86a009d549550d6047fd07 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 30 Mar 2014 21:21:24 +0100 Subject: py: Implement positional and keyword args via * and **. Extends previous implementation with * for function calls to * and ** for both function and method calls. --- tests/basics/fun-calldblstar.py | 17 +++++++++++++++++ tests/basics/fun-callstar.py | 20 ++++++++++++++++++++ tests/basics/fun-callstardblstar.py | 17 +++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 tests/basics/fun-calldblstar.py create mode 100644 tests/basics/fun-callstardblstar.py (limited to 'tests') diff --git a/tests/basics/fun-calldblstar.py b/tests/basics/fun-calldblstar.py new file mode 100644 index 000000000..aae9828cf --- /dev/null +++ b/tests/basics/fun-calldblstar.py @@ -0,0 +1,17 @@ +# test calling a function with keywords given by **dict + +def f(a, b): + print(a, b) + +f(1, **{'b':2}) +f(1, **{'b':val for val in range(1)}) + +# test calling a method with keywords given by **dict + +class A: + def f(self, a, b): + print(a, b) + +a = A() +a.f(1, **{'b':2}) +a.f(1, **{'b':val for val in range(1)}) diff --git a/tests/basics/fun-callstar.py b/tests/basics/fun-callstar.py index 49b40d959..255563b26 100644 --- a/tests/basics/fun-callstar.py +++ b/tests/basics/fun-callstar.py @@ -1,3 +1,5 @@ +# function calls with *pos + def foo(a, b, c): print(a, b, c) @@ -11,3 +13,21 @@ foo(1, 2, *[100]) # Iterator foo(*range(3)) + +# 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, *()) + +# Another sequence type +a.foo(1, 2, *[100]) + +# Iterator +a.foo(*range(3)) diff --git a/tests/basics/fun-callstardblstar.py b/tests/basics/fun-callstardblstar.py new file mode 100644 index 000000000..f2fd29107 --- /dev/null +++ b/tests/basics/fun-callstardblstar.py @@ -0,0 +1,17 @@ +# test calling a function with *tuple and **dict + +def f(a, b, c, d): + print(a, b, c, d) + +f(*(1, 2), **{'c':3, 'd':4}) +f(*(1, 2), **{['c', 'd'][i]:(3 + i) for i in range(2)}) + +# test calling a method with *tuple and **dict + +class A: + def f(self, a, b, c, d): + print(a, b, c, d) + +a = A() +a.f(*(1, 2), **{'c':3, 'd':4}) +a.f(*(1, 2), **{['c', 'd'][i]:(3 + i) for i in range(2)}) -- cgit v1.2.3