Pythonic: Difference between revisions

1,006 bytes added ,  18 May 2016
pyobj notes
(pyobj notes)
Line 107: Line 107:
G_struct = struct(G); # G converted to a struct
G_struct = struct(G); # G converted to a struct
</syntaxhighlight>}}
</syntaxhighlight>}}
==== 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.
{{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 should 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.
Other ideas:
2. Store the `repr` as a string in `x`.  This makes a copy of the object rather than a reference to the original object.
99

edits