Monday, April 20, 2009

Dispatch the method

# I hate it, when I see something like this:
class Ape:
    def __init__(self):
        self.cnt = 0
        
    def scream(self):
        if self.cnt == 0:
            print "uhgauhga!"
            self.cnt += 1
        else:
            print "ahahah!"
        return self
# Meh! You are programming OO.  Let me alone with your dirty flow logic.
Ape().scream().scream()

# This monkey is doing it right.
class Monkey:
    def scream(self):
        print "uhgauhga!"
        self.scream = self.scream2
        return self

    def scream2(self):
        print "ahahah!"
        return self

Monkey().scream().scream()

# Please just dispatch, after the first use!

No comments: