← Back to team overview

dolfin team mailing list archive

Re: pyDolfin Function inheritance

 

On Friday 30 January 2009 13:05:45 Evan Lezar wrote:
> Hi
>
> I have been away from my work for a while and am now trying to get my code
> working with the new dolfin including the concept of a FunctionSpace.  I am
> mostly working in Python.
>
> Consider the following python class
>
>
> *class MyFunction(Function):
>     def __init__(self, V, other_param):
>         Function.__init__(self, V)
>         self.set_other_param(other_param)
>
>     def set_other_param(self, param):
>         self._other_param = param*
>
> *    def get_other_param(self):
>         return self._other_param
> *
>
> The problem I have is that the code above (which worked up until now)
> results in the following error
>
>  File "/usr/local/lib/python2.5/site-packages/dolfin/cpp.py", line 5528, in
> <lambda>
>     __getattr__ = lambda self, name: _swig_getattr(self, Function, name)
>   File "/usr/local/lib/python2.5/site-packages/dolfin/cpp.py", line 34, in
> _swig_getattr
>     raise AttributeError,name
> AttributeError: set_other_param
>
> From what I can tell, the methods set_other_param and get_other_param are
> being handled using swig and not by calling the python methods as I would
> expect.
>
> I must be overlooking something in the new implementation, so if someone
> could point me in the right direction it would be much appreciated.

Hello Evan!

I agree that this looks a bit strange, and I am really not sure what happens.

Two things to try:
 1) When you define your own __init__ do not call Function.__init__. This is 
done behind the scens anyway.

The thing is that we dynamically create a python class behinds the scenes, 
(using metaclasses). This class inherit from both FFC.Function, cpp.Function 
(The dolfin compiled version) and dolfin.Function. This makes the call to 
Function.__init__ in your __init__ function possible ambigious. 

This is not what you expect as a experienced python user so we should consider 
putting in some warnings here. Will look into it...

2) Is it possible for you to use the compiled function interface? 

You should be able to set simple optional parameters here too. 
A very simple example would be:

   >>> f = Function(V,'A*sin(x[0])',{'A':4.5})
   >>> f.A
   4.5
   >>> f.A = 5.6

You can also compile more complex logics with more attributes other than float 
or int. See the docstring of Function user case 3)

Johan


Follow ups

References