[Python-Dev] trunc()

Raymond Hettinger python at rcn.com
Thu Jan 24 19:36:14 CET 2008


Can anyone explain to me why we need both trunc() and int()?

We used to be very resistant to adding new built-ins and
magic method protocols.  In days not long past, this would
have encountered fierce opposition.

ISTM that numbers.py has taken on a life of its own and
is causing non-essential offspring to sprout-up everywhere.

Also, it seems that ABC is starting to evolve from an optional
tool into something embedded in the language in a way that you
can't escape it.  

I would prefer that it not be on the list of concepts that a
beginner *must* know in order to be functional in the language. 

There are a handful of needs met by the numeric tower but
they only warrant minimal changes to the language.

Raymond


P.S. The docstring for trunc() makes it sound like an imprecisely
specified version of round().


----    trunc() looks like int() but it isn't    ---------------------------

>>> v = [-4.9, -4.5, -4.1, -4.0, -3.5, -0.0, 0.0, 3.5, 4.0, 4.1, 4.5, 4.9]
>>> map(int, v)
[-4, -4, -4, -4, -3, 0, 0, 3, 4, 4, 4, 4]
>>> map(trunc, v)
[-4, -4, -4, -4, -3, 0, 0, 3, 4, 4, 4, 4]
>>> trunc is int
False
>>> print trunc.__doc__
trunc(Real) -> Integral

returns the integral closest to x between 0 and x.


More information about the Python-Dev mailing list