Editing Pythonic
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 120: | Line 120: | ||
==== Python Objects in Octave ==== | ==== Python Objects in Octave ==== | ||
The {{Codeline|@ | The {{Codeline|@pyobj}} classdef class is intended to wrap arbitrary Python objects so they can be accessed an manipulated from within Octave. | ||
{{Code|avoiding garbage collection|<syntaxhighlight lang="octave" style="font-size:13px" line highlight="4"> | |||
pyexec('d = dict(one=1, two=2)') # create object in Python | |||
x = pyobj('d') # create pyobj wrapper for that object | |||
pyexec('d = []') # careful, don't lose the object to the GC | |||
x.keys() # list the keys of the dict | |||
clear x # now the object could be GCed | |||
</syntaxhighlight>}} | |||
One proposed way to do this: | |||
1. `x` stores the pointer to `d`. The `@pyobj` ctor creates a dummy reference to `d`, this prevents GC | |||
2. on deletion of x (`clear x`) we delete the dummy reference in Python. | |||
Notes: | |||
* Seems like the relevant "pointer" is {{Codeline|id()}}. Haven't seen yet how to access an object from its id, except that its a bad idea... | |||
* My plan to create a dict in Python, indexed by {{Codeline|hex(id(x))}}, maybe called {{Codeline|__InOct__}}. Then pass the id of the object to the {{Codeline|@pyobj/pyobj}} constructor. | |||
* Follow along and help out here: https://bitbucket.org/macdonald/pytave/commits/branch/cbm_pyobj | |||
* Rejected idea: store the `repr` as a string in `x`. But this makes a copy of the object rather than a reference to the original object. | |||
====== Interface design ====== | |||
* {{Codeline|pyeval}} should be modified to return a {{Codeline|@pyobj}} for things that it cannot convert to Octave-native types. See the `networkx` example above: `G` could be returned by `pyeval`. | |||
* {{Codeline|@pyobj/pyobj}} constructor would not normally be called by users. | |||
== Known Problems == | == Known Problems == |