From 6baf76e28b17055fc6e5a6c9560e756d32eaad5d Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Dec 2013 22:32:17 +0000 Subject: py: make closures work. --- tests/basics/tests/closure1.py | 16 ++++++++++++++++ tests/basics/tests/closure2.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/basics/tests/closure1.py create mode 100644 tests/basics/tests/closure2.py (limited to 'tests') diff --git a/tests/basics/tests/closure1.py b/tests/basics/tests/closure1.py new file mode 100644 index 000000000..610cb7002 --- /dev/null +++ b/tests/basics/tests/closure1.py @@ -0,0 +1,16 @@ +# closures + +def f(x): + y = 2 * x + def g(z): + return y + z + return g + +print(f(1)(1)) + +x = f(2) +y = f(3) +print(x(1), x(2), x(3)) +print(y(1), y(2), y(3)) +print(x(1), x(2), x(3)) +print(y(1), y(2), y(3)) diff --git a/tests/basics/tests/closure2.py b/tests/basics/tests/closure2.py new file mode 100644 index 000000000..e4e5154a9 --- /dev/null +++ b/tests/basics/tests/closure2.py @@ -0,0 +1,16 @@ +# closures; closing over an argument + +def f(x): + y = 2 * x + def g(z): + return x + y + z + return g + +print(f(1)(1)) + +x = f(2) +y = f(3) +print(x(1), x(2), x(3)) +print(y(1), y(2), y(3)) +print(x(1), x(2), x(3)) +print(y(1), y(2), y(3)) -- cgit v1.2.3