Can't override class |__new__

Aaron Brady castironpi at gmail.com
Thu Mar 5 04:29:04 EST 2009


On Mar 5, 2:27 am, jelle feringa <jelleferi... at gmail.com> wrote:
> Hi,
>
> I'm working with a C++ module ( CGAL, comp.geom. with exact arithmic )
> and am having troubles finding a way to override how the modules returns
> objects. What I'm trying to do is to extend the Facet class, but when I try
> to use my version of the class, the parent class is still being returned
>
> import CGAL
>
> CGAL.Facet = OtherFacet
> CGAL.Polyhedron.Facet = OtherFacet
>
> p = CGAL.Polyhedron_3()
> p.make_triangle()
>
> for f in p.facets:
>     print f
>
> <CGAL.Polyhedron.Facet object at 0x47ed50>
> # Here I was expecting a OtherFacet object
>
> Is there a way of getting around this, or is this behaviour hardcoded in the
> C++ module?
> Can I perhaps alter it using  the new module or __new__ method?
>
> Many thanks in advance,
>
> -jelle

Hello.  I don't have your module, but it sounds like the C is doing
something like this (pseudocode):

PyObject *make_triangle(...) {
    ...
    new_ob= PyObject_New( &CGAL_Polyhedron_Facet_Type );
    ...
}

Regardless of CGAL's dictionary, it instantiates a Facet.  What you
want it to do is (pseudocode):

PyObject *make_triangle(...) {
    ...
    /* get the current 'Facet' member */
    class_ob= PyObject_GetAttr( CGAL, "Facet" );
    /* instantiate it */
    new_ob= PyObject_New( class_ob );
    ...
}

Depending on the details, you may need only to cut-and-paste your own
'make_triangle' function, and just replace the line I showed.

Do you have a link to the source for CGAL?



More information about the Python-list mailing list