Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I usually do things like this with explicit <code>pythoncode</code> blocks in the interface file:</p> <pre><code>%pythoncode %{ def _special_python_member_function(self): print "hello" self.rotate() # some function of Sphere Sphere.new_functionality=_special_python_member_function %} </code></pre> <p>So you can add arbitrary python functionality to the class, on top of what the SWIG interface provides. You may want/need to <code>rename</code> some of the C functionality out the way but this can should get you all of the member <em>functions</em> you want.</p> <p>I've never tried to remap <code>__init__</code> in this way, so I don't know how that would behave. Assuming that it won't work, you won't be able to ensure that the python objects have a given internal state (member variables) at construction.</p> <p>What you will be forced to do is do lazy evaluation:</p> <pre><code>def function_that_depends_on_python_specific_state(self, *args): if not hasatttr( self, 'python_data'): self.python_data = self.make_python_data() # construct the relevant data pass # do work that involves the python specific data </code></pre> <p>and check for the existence of the python specific data. If there is just a few cases of this, I'd just put it in the functions as above. However, if that ends up being messy, you could modify <code>__getattr__</code> so that it constructs the python-specific data members as they are accessed.</p> <pre><code>def _sphere_getattr(self, name): if name=='python_data': self.__dict__[name]=self.make_python_data() return self.__dict__[name] else: raise AttributeError Sphere.__getattr__ = _sphere_getattr </code></pre> <p>IMHO, in the limit where you have a large amount of new functionality, and data that are independent of the underlying C implementation, you are in effect asking "How can I make my python Sphere class be a sub-class ofthe C Sphere class but keep them as the same type?"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload