Overriding member methods in __init__

c james cjames at callone.net
Mon Dec 3 08:53:31 EST 2007


Given a condition at the time a class is instantiated, I want to change
how __call__ is used.  From the example below, self.no is using self.yes
but self.__call__ is not.  Can someone please explain why?

EXAMPLE:
class YesNo(object):
    def __init__(self, which):
        self.no = self.yes
        self.__call__ = self.yes

    def yes(self, val):
        print 'Yes', val

    def no(self, val):
        print 'No', val

    def __call__(self, val):
        raise NotImplementedError()

>>> y = YesNo(True)
>>> y.yes('hello')
Yes hello
>>> y.no('hello')
Yes hello
>>> y('hello')
Traceback ....
Not ImplementedError




More information about the Python-list mailing list