[Python-Dev] trunc()
"Martin v. Löwis"
martin at v.loewis.de
Sun Jan 27 20:54:02 CET 2008
> 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?
Depends on what you compare to. Compared to int(), the advantage is
that trunc() sends a clear message what its semantics is.
> 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.
+1. Students just asked me why len() is not a method, and I didn't know
a good answer; the same holds for many other builtins. This is a clear
candidate for a method, IMO.
> 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").
Actually, it works fine for float literals:
py> 2.45.round()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'float' object has no attribute 'round'
It's only int literals which have a problem with methods, but then,
you won't call any of these on an int literal, normally.
Regards,
Martin
More information about the Python-Dev
mailing list