[Pyrex] self-referencing extension types

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Sep 22 04:39:25 CEST 2005


Nitin Madnani wrote:

> This does work fine but I was curious if there is an equivalent way  to 
> this sort of self-referential thing using extension types.

Certainly. Here's an example:

   cdef class Spam:
     cdef int tons
     cdef Spam more

   def test():
     cdef Spam s1, s2
     s1 = Spam()
     s2 = Spam()
     s1.tons = 42
     s2.tons = 17
     s1.more = s2
     s2.more = s1
     print s1.more.more.tons

The only tricky part is that if you have two extension types that
need to point to each other, you'll have to forward-declare one
of them. See "Forward-Declaring Extension Types" in the "Extension
Types" section of the Language Overview.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Pyrex mailing list