# [Tutor] Problem with default arguments for function

Michael H. Goldwasser goldwamh at slu.edu
Mon Nov 12 02:10:28 CET 2007

```
Dick,

Another typical strategy is to use some prescribed special value for
the precision parameter to designate the desire for full precision.
For example, since precisions should presumably be positive, one could
design this function as:

def fact(n, precision=15):
"""compute n!.

precision    the minimum desired precision.
If -1 is specified, computed to full precision.
"""
# ...
if precision == -1:
precision = n * 10  # insures that for n < 1 billion, ...
# ...

If you are not happy with the oddity of -1 (or in cases where -1 might
be a legitimate parameter value), you can pick the flag from a
different data type.  In this case, perhaps None would be a more
natural way to say that you do not want any limit on the precision.
So this could be coded as

def fact(n, precision=15):
"""compute n!.

precision    the minimum desired precision.
If None is specified, computed to full precision.
"""
# ...
if precision is None:
precision = n * 10  # insures that for n < 1 billion, ...
# ...

Looking at your examples, this should (unteste) behave as:

# 1 (precision default overridden and set to 20)
>>> print fact(50, 20)
3.0414093201713378044e+64

# 2 (without explicit value, precision defaults to 15)
>>> print fact(50)
3.04140932017134e+64

# 3 (explicitly says not to limit precision)
>>> print fact(50, None)
30414093201713378043612608166064768844377641568960512000000000000

With regard,
Michael

On Sunday November 11, 2007, Dick Moores wrote:

>    def fact(n, precision=15, full=False):
>         """
>         compute n!
>         """

>    And a couple of example uses that illustrate my problem:
>
>    # 1 (precision default overridden and set to 20; full left at default of False)
>     >>> print fact(50, 20)
>    3.0414093201713378044e+64
>
>    # 2 (both precision and full left at their defaults)
>     >>> print fact(50)
>    3.04140932017134e+64
>
>    # 3 (full set to True, forcing precision to be specified--but
>    irrelevant what it is set to)
>     >>> print fact(50, 3, True)
>    30414093201713378043612608166064768844377641568960512000000000000
>
>    # 4 (see # 3)
>     >>> print fact(50, 30, True)
>    30414093201713378043612608166064768844377641568960512000000000000
>
>    And if the function is rewritten as def fact(n, full=False, precision=15)
>    there would be the analogous problem involving full.
>
>    Is there a way to solve this problem of the unnecessary setting of
>    the 2nd argument?
>
>    Thanks,
>
>    Dick Moores
>
>    _______________________________________________
>    Tutor maillist  -  Tutor at python.org
>    http://mail.python.org/mailman/listinfo/tutor

```