On Tue, Dec 02, 2008 at 06:19:18PM +0100, Martin Sandve Alnæs wrote: > 2008/12/2 Anders Logg <[EMAIL PROTECTED]>: > > On Tue, Dec 02, 2008 at 12:12:14PM +0100, Anders Logg wrote: > >> I've tried adding a new class Constant in function.py: > >> > >> class Constant(ffc.Constant, dolfin.cpp_Function): > >> > >> def __init__(self, domain, value): > >> "Create constant-valued function." > >> print domain > >> print value > >> #ffc.Constant.__init__(self, domain) > >> #dolfin.cpp_Constant.__init__(self, value) > >> > >> But I get the following error message: > >> > >> File > >> > >> "/scratch/fenics/dolfin/dolfin-dev/local/lib/python2.5/site-packages/dolfin/function.py", > >> line 411, in <module> > >> class Constant(ffc.Constant, dolfin.cpp_Function): > >> TypeError: Error when calling the metaclass bases > >> function() argument 1 must be code, not str > >> > >> How is this possible? There should be no metaclasses involved here > >> (except the built-in Python metaclass type that is always there). > > > > I get this error even if I just try to create a class named anything > > that inherits from ffc.Constant. > > > > Does the metaclass construction in function.py have side-effects? > > > > I don't remember if we discussed this before, but would it be possible > > (at least simpler) to instead define a simple Python function that > > returns a "function" instance: > > > > class FooFunction(ffc.Function, ...): > > ... > > class BarFunction(dolfin.Function, ...): > > ... > > > > def Function(V, *arg): > > > > if foo: > > return FooFunction(...) > > elif bar: > > return BarFunction(...) > > > > This seems to be an easier solution. It would still be dynamic. > > The __metaclass__ attribute of Function doesn't have to be a type, it > can be any callable. > > > The only drawback would be that we can't do > > > > isinstace(v, Function) > > As mentioned before, I see that as an advantage ;)
But if all created classes inherit from FunctionBase, it would be the
same as for UFL/FFC where all elements inherit from FiniteElementBase:
if isinstance(v, FunctionBase):
...
The advantage would be that the code would be easier to understand (at
least I would understand it). Maybe I would also be able to understand
the current implementation if it was reorganized a bit (as suggested
earlier with __new_discrete_function etc).
Where is Hake when you need him? :-)
--
Anders
signature.asc
Description: Digital signature
_______________________________________________ DOLFIN-dev mailing list [email protected] http://www.fenics.org/mailman/listinfo/dolfin-dev
