Python Sanity Proposal: Type Hinting Solution

Marko Rauhamaa marko at pacujo.net
Fri Jan 23 18:52:45 EST 2015


Mario Figueiredo <marfig at gmail.com>:
> Much better is:
>
>       def myfunction(arg1, arg2):
>       """
>       Normal docstring...
>       @typehint: (str, int) -> bool
>       """ 
>           return True 

I seem to remember an idea floated on the Scheme mailing list of using
assertions for such a purpose:

   def myfunction(arg1, arg2):
       assert isinstance(arg1, str) and isinstance(arg2, int)
       return True

The advantage is that the assertions can be as complex as you'd like:

   def weekday(day):
       assert isinstance(day, int) and 0 <= day <= 6
       ...

   def str_product(x, y):
       assert isinstance(x, int) and isinstance(y, str) or \
           isinstance(x, str) and isinstance(y, int)
       ...

Also, they have always been present in the language and assertion
semantics is fully compatible with static analysis.

Only I'd hate if that style became standard boilerplate...


Marko



More information about the Python-list mailing list