[Python-Dev] trunc() (original) (raw)

"Martin v. Löwis" martin at v.loewis.de
Thu Jan 24 20:36:18 CET 2008


trunc() has well-defined semantics -- it takes a Real instance and converts it to an Integer instance using round-towards-zero semantics.

No. trunc calls trunc, which does whatever it pleases to do.

class A: ... def trunc(self): ... return 0 ... a=A() trunc(a) 0

int() has undefined semantics -- it takes any object and converts it to an int (a concrete type!) using whatever rules it likes -- the definition of int is up to whatever the source type likes to do. For float this has been defined the same as trunc() above, but for other types, who knows! int() of a string does something completely different.

But why is that a reason to keep trunc()? If you only ever want to convert floats to ints, you can use either one, with no difference. int() does not have undefined semantics, for floats, it converts it to an integer using round-towards-zero semantics.

Regards, Martin



More information about the Python-Dev mailing list