Comparisons and sorting of a numeric class....

Chris Angelico rosuav at gmail.com
Sat Jan 24 00:43:32 EST 2015


On Sat, Jan 24, 2015 at 4:01 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> # Untested
> class B4(IntEnum):
>     F1 = 0
>     F2 = 1
>     F3 = 2
>     T  = 3
>     def __bool__(self):
>         return self is B4.T
>     def __str__(self):
>         if self is B4.F1: return "Certainly False"
>         if self is B4.F2: return "Maybe False"
>         if self is B4.F3: return "Maybe True"
>         if self is B4.T: return "Certainly True"

I keep expecting to hear Bluebottle asking about why we didn't figure
this out B4... anyway.

class B4(IntEnum):
    CertainlyFalse = 0
    MaybeFalse = 1
    MaybeTrue = 2
    CertainlyTrue  = 3
    def __bool__(self):
        return self is B4.CertainlyTrue

Now you don't need a __str__ function. :)

ChrisA



More information about the Python-list mailing list