On Sat, Jul 24, 2010 at 2:29 PM, Burcin Erocal <[email protected]> wrote:
> Hi,
>
> At Sage Days 24, I learned that Python allows the user to do arithmetic
> with bools:
>
> In [1]: 5+True
> Out[1]: 6
>
> In [2]: True + False
> Out[2]: 1
>
> In [3]: 5+False
> Out[3]: 5
>
> Sage seems to follow this convention as well:
>
> sage: 5 + True
> 6
> sage: 5. - True
> 4.00000000000000
>
> I can't see any use cases for this convention. I believe all these
> examples should just raise a TypeError. IMHO, code relying on this
> feature is very likely to be buggy.
>
> Apparently the symbolic ring doesn't handle things so well [1], but I'm
> not decided what the "fix" should be. :)
>
> [1] http://trac.sagemath.org/sage_trac/ticket/9560
>
> Any comments?

For whatever reason, bools subclass int,

sage: isinstance(True, int)
True

so to accept ints and reject bools, we'd have to do a lot of special
casing. This is useful, for example

sage: sum(is_prime(p) for p in range(100))
25

- Robert

-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to