[Python-Dev] trunc()

Daniel Stutzbach daniel at stutzbachenterprises.com
Sun Jan 27 19:55:26 CET 2008


On Jan 27, 2008 10:43 AM, Guido van Rossum <guido at python.org> wrote:
> - trunc(), round(), floor() and ceil() should all be built-ins,
> corresponding to __trunc__, __round__, __floor__ and __ceil__. Then we
> have the four standard ways to go from Reals to Integers, which are
> properly extensible for folks who write their own number types. (We
> can't control how folks implement __round__, but we can document
> expected behavior -- that's how we treat __add__ and all other
> operators too, after all.)

If I'm following this discussion properly, the advantage of trunc() is
that a Real-class-that-isn't-float can define a __trunc__ that can
return an Integer-class-that-isn't-int, right?

In that case, why not have the Real ABC grow trunc(), ceil(), floor(),
and round() methods (replacing the __ varieties), and get rid of the
builtins/math-module functions?

x.trunc() is just as clear as trunc(x), and doesn't require a builtin.

The syntax when used on float literals is ugly ("2.56 .round()"), but
there's no use case for these methods on literals (just write "3").

2to3 could handle this conversion pretty easily in most cases, so
there won't be much breakage.

-- 
Daniel Stutzbach, Ph.D.             President, Stutzbach Enterprises LLC


More information about the Python-Dev mailing list