diff options
author | Damien George <damien.p.george@gmail.com> | 2013-12-30 22:32:17 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2013-12-30 22:32:17 +0000 |
commit | 6baf76e28b17055fc6e5a6c9560e756d32eaad5d (patch) | |
tree | f4245858b6755d6731a7ec6c9f0a6fc49901eb5b /tests | |
parent | 8cc96a35e532ef999e5a3739deeb44f51a80744b (diff) | |
download | micropython-6baf76e28b17055fc6e5a6c9560e756d32eaad5d.tar.gz micropython-6baf76e28b17055fc6e5a6c9560e756d32eaad5d.zip |
py: make closures work.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/basics/tests/closure1.py | 16 | ||||
-rw-r--r-- | tests/basics/tests/closure2.py | 16 |
2 files changed, 32 insertions, 0 deletions
diff --git a/tests/basics/tests/closure1.py b/tests/basics/tests/closure1.py new file mode 100644 index 0000000000..610cb70020 --- /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 0000000000..e4e5154a94 --- /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)) |