On 28 October 2010 19:52, Simon Anders <[email protected]> wrote: > Hi > > I might have found a subtle bug regarding Cython's handling of > polymorphic classes: When a class contains a field, and a daughter class > overlays the field with a property, a method of the base class will fail > to notice this when called for an object of the daughter class. > > If this sounded convoluted, an example might be clearer: > > --8<-- > cdef class A( object ): > > cdef str me > > def __init__( self ): > self.me = "A" > > def whoami( self ): > return self.me > > cdef class B( A ): > > �...@property > def me( self ): > return "B" > > def main(): > b = B() > print b.me > print b.whoami() > --8<-- > > The output of main() is: > B > A >
Expected. > > However, the equivalent Python code, i.e., the same file, without the > 'cdef's before the 'class' statement and without the line 'cdef str me', > produces an error, namely: > Actually, you Python code is not equivalent. cdef classes are extension types, and they behave different. if you remove "cdef", you will get regular Python classes, and the code will work as you are expecting. > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "test.pyx", line 16, in test.main (test.c:489) > b = B() > File "test.pyx", line 4, in test.A.__init__ (test.c:380) > self.me = "A" > AttributeError: can't set attribute > > > I haven't tested it, but I'd guess that, had I provided a setter method > for the property, too, A.__init__ would not have used it when called for > a B object. > > > Cheers > Simon > > > +--- > | Dr. Simon Anders, Dipl.-Phys. > | European Molecular Biology Laboratory (EMBL), Heidelberg > | office phone +49-6221-387-8632 > | preferred (permanent) e-mail: [email protected] > _______________________________________________ > Cython-dev mailing list > [email protected] > http://codespeak.net/mailman/listinfo/cython-dev > -- Lisandro Dalcin --------------- CIMEC (INTEC/CONICET-UNL) Predio CONICET-Santa Fe Colectora RN 168 Km 472, Paraje El Pozo Tel: +54-342-4511594 (ext 1011) Tel/Fax: +54-342-4511169 _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
