On Wednesday 02 December 2015 18:51:03 Aymeric Augustin wrote:
> 
> We could implement a property that returns an object:
> 
> - that is still callable, for backwards compatibility
> - that evaluates correctly in a boolean context
> 
> Then we can consider deprecating the ability to call it.
> 
> 
> class CallableBool:
> 
>     def __init__(self, value):
>         self.value = value
> 
>     def __bool__(self):
>         return self.value
> 
>     def __call__(self):
>         return self.value
> 
>     def __repr__(self):
>         return 'CallableBool(%r)' % self.value
> 
> CallableFalse = CallableBool(False)
> 
> CallableTrue = CallableBool(True)
> 

More general alternative:

        class IdempotentCallMixin(object):
                def __call__(self): return self

However, you can't extend bool, so

        class CallableBool(IdempotentCallMixin, bool): pass

does not work, and you'd have to do something like

        class CallableBool(IdempotentCallMixin, int): pass

which is less nice (because of __repr__ etc)

Shai

Reply via email to