-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_oop.ring
More file actions
49 lines (38 loc) · 1.19 KB
/
08_oop.ring
File metadata and controls
49 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
08 - Object-Oriented API
Use the Ring wrapper classes for a cleaner interface.
*/
load "python.ring"
# The Python class auto-initializes the interpreter
py = new Python()
? "Python version: " + py.version()
? ""
# Execute and evaluate
py.exec("import math")
py.exec("result = math.factorial(10)")
? "10! = " + py.getVar("result")
? "sqrt(2) = " + py.callFuncArgs("math.sqrt", [2])
? ""
# PyModule — wraps a Python module
math = new PyModule("math")
? "math.pi: " + math.getattr("pi")
? "math has sqrt: " + math.hasattr("sqrt")
? "math type: " + math.type()
? ""
# PyList, PyDict, PyTuple
mylist = new PyList([10, 20, 30])
? "PyList: " + mylist.str() + " len=" + mylist.len()
mydict = new PyDict([["name", "Ring"], ["year", 2016]])
? "PyDict: " + mydict.str() + " len=" + mydict.len()
mytuple = new PyTuple([1, 2, 3])
? "PyTuple: " + mytuple.str() + " len=" + mytuple.len()
? ""
# PyBool, PyNone
pynone = new PyNone()
? "PyNone type: " + pynone.type()
pytrue = new PyBool(true)
? "PyBool(true) value: " + pytrue.value()
# PyValue — convert any Ring value to Python object
pyval = new PyValue("Hello from Ring!")
? "PyValue str: " + pyval.str()
? "PyValue type: " + pyval.type()