Skip to content

Commit eedd6a0

Browse files
committed
Add eval examples
1 parent 7702bd7 commit eedd6a0

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

function-passing-eval.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
"""
3+
This example demonstrates passing functions as arguments. Here, a
4+
function is deinfed in JavaScript which expects a function as an
5+
argument. Then that JavaScript function is executed from Python and
6+
the Python `print` function is passed as an argument.
7+
"""
8+
9+
import pythonmonkey as pm
10+
hello = pm.eval("(func) => { func('Hello, World!'); }")
11+
hello(print) # this outputs "Hello, World!"
12+

hello-world-eval.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
"""
3+
This example returns a "HELLO, WORLD!" string created in JavaScript
4+
using `pm.eval` for execting the JavaScript code.
5+
The string is set to uppercase using JavaScript's `toUpperCase`
6+
function.
7+
"""
8+
9+
import pythonmonkey as pm
10+
hello = pm.eval(" 'Hello, World!'.toUpperCase(); ")
11+
print(hello) # this outputs "HELLO, WORLD!"
12+

0 commit comments

Comments
 (0)