Arnaud Delobelle wrote:
> Why not simply do:
>
> class YesNo(object):
> def __init__(self, which):
> self.yesno = which and self.yes or self.no
> def yes(self, val):
> print 'Yes', val
> def no(self, val):
> print 'No', val
> def __call__(self, val):
>
On Dec 3, 3:36 pm, Hrvoje Niksic <[EMAIL PROTECTED]> wrote:
> Allowing instance lookup of __call__ would slow down normal uses of
> the internal __call__ mechanism. Since it used for all function and
> method calls, it needs to remain extremely fast. If you're really
> worried about performance,
c james <[EMAIL PROTECTED]> writes:
>> 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):
>>
c james <[EMAIL PROTECTED]> wrote:
> Thanks, I was trying to eliminate another level of indirection with a
> test at each invocation of __call__
>
>
Try using different subclasses for each variant:
class YesNo(object):
def __new__(cls, which, *args, **kw):
if cls is YesNo:
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__
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.
>