[docs] Inaccuracy in the description of factorial function

Zachary Ware zachary.ware+pydocs at gmail.com
Mon Dec 29 08:23:08 CET 2014


Hi Sergey,

On Sat, Dec 27, 2014 at 12:51 PM, Sergey Skovorodkin
<ls.sergey at gmail.com> wrote:
> Documentation of math.factorial function says [1]:
>> Raises ValueError if x is not integral or is negative.
>
> It should be changed to:
>> Raises ValueError if x is negative or TypeError if it is not integral.
>
> It's applicable for Python versions 2.7 and 3.4. I didn't check it for other
> versions.

Thanks for the report, but I don't think any change should be made.  See below:

   >>> math.factorial(-3)  # negative x
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   ValueError: factorial() not defined for negative values
   >>> math.factorial(3.14)  # non-integral x
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   ValueError: factorial() only accepts integral values
   >>> math.factorial('nope')  # non-number x
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: an integer is required (got type str)

The TypeError raised in the non-number x case is just like the
TypeError that any function might raise if it receives an argument of
the wrong type.  We generally don't document places that could raise
TypeError because nearly any function could, and a TypeError generally
indicates a programming error rather than something that can (or
should) be recovered from.

Regards,
-- 
Zach


More information about the docs mailing list