Pythonic: Difference between revisions

1,554 bytes removed ,  19 August 2016
→‎Python Objects in Octave: clean-up pyobj section: this is basically working, rather than design
(→‎Known Problems: add function handle problems)
(→‎Python Objects in Octave: clean-up pyobj section: this is basically working, rather than design)
Line 119: Line 119:
==== Python Objects in Octave ====
==== Python Objects in Octave ====


The {{Codeline|@pyobj}} classdef class is intended to wrap arbitrary Python objects so they can be accessed an manipulated from within Octave.
The {{Codeline|@pyobject}} class wraps arbitrary Python objects so they can be accessed and manipulated from within Octave.  In most cases, these are created automatically.
 
{{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 ==
99

edits