Overriding member methods in __init__
c james
cjames at callone.net
Mon Dec 3 09:35:38 EST 2007
Bruno Desthuilliers wrote:
> c james a écrit :
>> 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?
>
> IIRC, you can't override __magic__ methods on a per-instance basis.
> This should do the trick:
>
> class YesNo(object):
> def __init__(self, which):
> self.which = which
>
> def __call__(self, val):
> return (self.no, self.yes)[self.which](val)
>
> def yes(self, val):
> print 'Yes', val
>
> def no(self, val):
> print 'No', val
Thanks, I was trying to eliminate another level of indirection with a
test at each invocation of __call__
More information about the Python-list
mailing list