summaryrefslogtreecommitdiff
path: root/weave/examples/object.py
diff options
context:
space:
mode:
authorEric Jones <eric@enthought.com>2002-10-20 22:25:08 +0000
committerEric Jones <eric@enthought.com>2002-10-20 22:25:08 +0000
commit64a54bfc3429dd80c190e16c00a33aa6012c948f (patch)
tree82cd1c2c3ab3535b68522fcbe48dd382df354acb /weave/examples/object.py
parent188c4df015fb031c9485d3640bc3dfa1b3e779c1 (diff)
downloadnumpy-64a54bfc3429dd80c190e16c00a33aa6012c948f.tar.gz
added examples for the py::object C++ object.
Diffstat (limited to 'weave/examples/object.py')
-rw-r--r--weave/examples/object.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/weave/examples/object.py b/weave/examples/object.py
new file mode 100644
index 000000000..b69adbbe6
--- /dev/null
+++ b/weave/examples/object.py
@@ -0,0 +1,51 @@
+# h:\wrk\scipy\weave\examples>python object.py
+# initial val: 1
+# inc result: 2
+# after set attr: 5
+
+import weave
+
+#----------------------------------------------------------------------------
+# get/set attribute and call methods example
+#----------------------------------------------------------------------------
+
+class foo:
+ def __init__(self):
+ self.val = 1
+ def inc(self,amount):
+ self.val += 1
+ return self.val
+obj = foo()
+code = """
+ int i = obj.attr("val");
+ std::cout << "initial val: " << i << std::endl;
+
+ py::tuple args(1);
+ args[0] = 2;
+ i = obj.mcall("inc",args);
+ std::cout << "inc result: " << i << std::endl;
+
+ obj.set_attr("val",5);
+ i = obj.attr("val");
+ std::cout << "after set attr: " << i << std::endl;
+ """
+weave.inline(code,['obj'])
+
+#----------------------------------------------------------------------------
+# indexing of values.
+#----------------------------------------------------------------------------
+from UserList import UserList
+obj = UserList([1,[1,2],"hello"])
+code = """
+ int i;
+ // find obj length and accesss each of its items
+ std::cout << "UserList items: ";
+ for(i = 0; i < obj.length(); i++)
+ std::cout << obj[i] << " ";
+ std::cout << std::endl;
+ // assign new values to each of its items
+ for(i = 0; i < obj.length(); i++)
+ obj[i] = "goodbye";
+ """
+weave.inline(code,['obj'])
+print "obj with new values:", obj \ No newline at end of file