[Python-ideas] Specifying constants for functions

Terry Reedy tjreedy at udel.edu
Tue Oct 27 20:57:31 EDT 2015


On 10/27/2015 7:36 PM, Steven D'Aprano wrote:

> The unobvious and ugly way is to put the calculation in the function
> declaration as a default value:
>
> def spam(SENTINEL=next_prime_number(2**512)):
>      ...
>
>
> which complicates the function signature and risks errors if the caller
> accidentally calls the function with too many arguments.

Making constant names keyword only avoids the 'too many arguments' problem.

 >>> def spam(*, _SETINAL=object): pass

 >>> spam(1)
Traceback (most recent call last):
   File "<pyshell#3>", line 1, in <module>
     spam(1)
TypeError: spam() takes 0 positional arguments but 1 was given

Writing 'spam(_SETINAL=3)' would not be an accident ;-)

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list