1# mode: run 2 3 4cdef class Spam: 5 6 cdef int tons 7 8 cdef void add_tons(self, int x): 9 self.tons += x 10 11 cdef void eat(self): 12 self.tons = 0 13 14 def lift(self): 15 print self.tons 16 17 18cdef class SubSpam(Spam): 19 20 cdef void add_tons(self, int x): 21 self.tons += 2 * x 22 23 24def test_spam(): 25 """ 26 >>> test_spam() 27 5 28 0 29 20 30 5 31 """ 32 cdef Spam s 33 cdef SubSpam ss 34 s = Spam() 35 s.eat() 36 s.add_tons(5) 37 s.lift() 38 39 ss = SubSpam() 40 ss.eat() 41 ss.lift() 42 43 ss.add_tons(10) 44 ss.lift() 45 46 s.lift() 47 48 49cdef class SpamDish: 50 cdef int spam 51 52 cdef void describe(self): 53 print "This dish contains", self.spam, "tons of spam." 54 55 56cdef class FancySpamDish(SpamDish): 57 cdef int lettuce 58 59 cdef void describe(self): 60 print "This dish contains", self.spam, "tons of spam", 61 print "and", self.lettuce, "milligrams of lettuce." 62 63 64cdef void describe_dish(SpamDish d): 65 d.describe() 66 67 68def test_spam_dish(): 69 """ 70 >>> test_spam_dish() 71 This dish contains 42 tons of spam. 72 This dish contains 88 tons of spam and 5 milligrams of lettuce. 73 """ 74 cdef SpamDish s 75 cdef FancySpamDish ss 76 s = SpamDish() 77 s.spam = 42 78 ss = FancySpamDish() 79 ss.spam = 88 80 ss.lettuce = 5 81 describe_dish(s) 82 describe_dish(ss) 83