Avoiding argument checking in recursive calls
Paul Rubin
http
Wed Feb 11 02:50:43 EST 2009
Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
> I'd write nested functions:
>
> def fact(n):
> if n < 0: raise ValueError
> def f1(n):
> return 1 if n==0 else n*f1(n-1)
> return f1(n)
I forgot to add: all these versions except your original one should
add a type check if you are trying to program defensively. Otherwise
they can recurse infinitely if you give a positive non-integer arg like
fact(3.5).
More information about the Python-list
mailing list