Editing Pythonic

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 119: Line 119:
 
==== Python Objects in Octave ====
 
==== Python Objects in 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.
+
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 ==

Please note that all contributions to Octave may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Octave:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Templates used on this page: