[Pyrex] self-referencing extension types

Lenard Lindstrom len-l at telus.net
Thu Sep 22 02:04:25 CEST 2005


def class stack:
    cdef public long n
    cdef public double f
    cdef stack _s

    def __init__(self, long n = 0, double f = 0.0, stack s = None):
        self.n = n
        self.f = f
        self._s = s

    property s:
        def __get__(self):
            return self._s

        def __set__(self, stack s):
            self._s = s

def test():
    cdef stack s1, s2, s3

    s1 = stack(33, 4.5)
    s2 = stack(34, 5.6)

    # They point to each other
    s1.s = s2
    s2.s = s1

    # See if it worked ..
    s3 = s1.s
    print s3.n

    # Get rid of the circlular reference
    # (though it would get garbage collected eventually)
    s1.s = None

Of course this is a pure extension type and not a wrapper for a C structure. You 
could not use it outside of Python.

Lenard Lindstrom
<len-l at telus.net>

On 21 Sep 2005 at 12:08, Nitin Madnani wrote:

> I Just realized that I *really* want to do this with extension types  
> since i want to return objects of type 'stack' from functions that I  
> will write. I can't seem to do that with the way I am set up now (see  
> below or my previous post).
> 
> Thanks !
> Nitin
> 
> On Sep 21, 2005, at 12:32 AM, Nitin Madnani wrote:
> 
> > Sorry to flood the mailing list with my questions but how do I do  
> > something like this in pyrex with extension types:
> >
> > cdef extern from "foo.h":
> >     ctypedef struct stack:
> >         long n
> >         double f
> >         stack* s
> >
> > def test():
> >     cdef stack s1, s2
> >     cdef stack* s3
> >
> >     s1.n = 33
> >     s1.f = 4.5
> >
> >     s2.n = 34
> >     s2.f = 5.6
> >
> >     # They point to each other
> >     s1.s = &s2
> >     s2.s = &s1
> >
> >     # See if it worked ..
> >     s3 = s1.s
> >     print s3.n
> >
> >
> > This does work fine but I was curious if there is an equivalent way  
> > to this sort of self-referential thing using extension types.
> > Thanks !
> > Nitin
> >
> > _______________________________________________
> > Pyrex mailing list
> > Pyrex at lists.copyleft.no
> > http://lists.copyleft.no/mailman/listinfo/pyrex
> >
> 
> 
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex
> 





More information about the Pyrex mailing list