Pythonic: Difference between revisions

1,867 bytes added ,  16 August 2016
(→‎Features: list container indexing features)
(→‎Known Problems: new section)
Line 150: Line 150:


* {{Codeline|@pyobj/pyobj}} constructor would not normally be called by users.
* {{Codeline|@pyobj/pyobj}} constructor would not normally be called by users.
== Known Problems ==
This section documents some known problems or limitations of the Python calling interface, usually due to a limitation of the Octave language itself or a bug in Octave that needs some attention.
<ul>
<li>Assignment to <tt>dict</tt> or other mapping object with string keys fails. The following syntax produces an error:
{{Code||<syntaxhighlight lang="octave">
d = py.dict ();
d{"one"} = 1;
</syntaxhighlight>}}
Use the {{Codeline|__setitem__}} method instead as a workaround:
{{Code||<syntaxhighlight lang="octave">
d.__setitem__ ("one", 1);
</syntaxhighlight>}}
The reason is because Octave strings are interpreted as arrays in many contexts, and this syntax is parsed by Octave as an attempt to assign to 3 elements of an object.</li>
<li>Element indexing on a <tt>list</tt> or other sequence object with a range or set of indices doesn't return the right number of output arguments. Element indexing should return as many values as were indexed, each assigned to the <tt>ans</tt> variable in turn, or be able to wrap the return list in a cell array, as shown here:
{{Code||<syntaxhighlight lang="octave">
x = py.list ({1, 2, 3, 4, 5, 6});
x{1:3}
y = {x{1:3}};
</syntaxhighlight>}}
Instead of the expected behavior, a cell array of elements is returned in both cases. This is Octave bug {{Bug|48693}}. The following patterns for assigning the results do work instead:
{{Code||<syntaxhighlight lang="octave">
[a, b, c] = x{1:3}
[y{1:3}] = x{1:3};
</syntaxhighlight>}}</li>
<li>Objects are not deleted because object destructors are not called by Octave when objects are cleared or go out of scope. For the Python interface, this means that the internal store of objects will continue to grow and objects will persist indefinitely even when all Octave references to a given Python object are gone. This is Octave bug {{Bug|46497}}.</li>
</ul>


== Pytave ==
== Pytave ==


This project is currently derived from an earlier project called Pytave, which was developed to work in the opposite direction, to allow Python to call Octave functions on an embedded Octave interpreter. The bulk of the project is in the code to convert between Octave and Python data types, so most of that is reusable and serves both purposes. As a side goal, we may continue to maintain the Python wrapper around Octave and incorporate that into Octave as well, so that Octave can provide its own native Python module.
This project is currently derived from an earlier project called Pytave, which was developed to work in the opposite direction, to allow Python to call Octave functions on an embedded Octave interpreter. The bulk of the project is in the code to convert between Octave and Python data types, so most of that is reusable and serves both purposes. As a side goal, we may continue to maintain the Python wrapper around Octave and incorporate that into Octave as well, so that Octave can provide its own native Python module.
296

edits