# HG changeset patch # User Abhinav Tripathi # Date 1469082815 25200 # Node ID 9da4ca336ddadbcc4183d332b683f3cc8bdaae1c # Parent 237b71aaf6e7396a45660d4671178c7f985d3754 Add 'length' property to @pyobject (fixes issue #9) * @pyobject/pyobject.m: Add the new length property and tests diff -r 237b71aaf6e7 -r 9da4ca336dda @pyobject/pyobject.m --- a/@pyobject/pyobject.m Wed Jul 20 14:24:09 2016 -0700 +++ b/@pyobject/pyobject.m Wed Jul 20 23:33:35 2016 -0700 @@ -118,6 +118,14 @@ vargout = {s}; endif endfunction + + function len = length (x) + try + len = pycall ("len", x); + catch + len = 1; + end_try_catch + endfunction endmethods endclassdef @@ -126,3 +134,13 @@ %! pyexec ("import sys") %! A = pyeval ("sys"); %! assert (isa (A, "pyobject")) + +%!test +%! pyobj = pyeval ("{1:2, 2:3, 3:4}"); +%! assert (isa (pyobj, "pyobject")) +%! assert (length (pyobj), 3) + +%!test +%! pyexec ("import sys"); +%! pyobj = pyeval ("sys"); +%! assert (length (pyobj), 1)