summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2013-12-30 22:32:17 +0000
committerDamien George <damien.p.george@gmail.com>2013-12-30 22:32:17 +0000
commit6baf76e28b17055fc6e5a6c9560e756d32eaad5d (patch)
treef4245858b6755d6731a7ec6c9f0a6fc49901eb5b /tests
parent8cc96a35e532ef999e5a3739deeb44f51a80744b (diff)
downloadmicropython-6baf76e28b17055fc6e5a6c9560e756d32eaad5d.tar.gz
micropython-6baf76e28b17055fc6e5a6c9560e756d32eaad5d.zip
py: make closures work.
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/tests/closure1.py16
-rw-r--r--tests/basics/tests/closure2.py16
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))