[Python-ideas] Optional static typing -- the crossroads

MRAB python at mrabarnett.plus.com
Sun Aug 17 17:27:36 CEST 2014


On 2014-08-17 15:37, Stefan Behnel wrote:
> Greg Ewing schrieb am 17.08.2014 um 12:31:
>> Nicholas Cole wrote:
>>> On Sun, Aug 17, 2014 at 3:08 AM, Steven D'Aprano wrote:
>>>
>>>>    def __init__(self,
>>>>            description:str, sec_code:str,
>>>>            vendor_name:str, vendor_inv_num:str,
>>>>            vendor_rtng:str, vendor_acct:str,
>>>>            transaction_code:str, vendor_acct_type:str,
>>>>            amount:int, payment_date:Any)->None:
>>>
>>> I had to stare at that block of
>>> code for a long time to see how many and what type of arguments it
>>> called.
>>
>> Pascal's function signature syntax had a nice feature
>> that everyone else seems to have forgotten about. If you
>> had multiple parameters of the same type, you only had
>> to write the type once:
>>
>>     procedure Init(description, sec_code, vendor_name,
>>         vendor_inv_num, vendor_rtng, vendor_acct,
>>         transaction_code, vendor_acct_type, amount: str;
>>         payment_date: Any)
>>
>> Disappointingly, Python's annotations make the same
>> blunder as C, and most other languages since, in
>> requiring each parameter to have its own individual
>> annotation.
>
> The difference is that Pascal requires type declarations whereas they are
> purely optional in Python. That makes the case that they are "missing" the
> right thing to optimise for, i.e. they should be explicit where they are
> and not take away space where they are not. Allowing argument sequences
> under a single type annotation would require some kind of marker for either
> that list or for the other arguments that are not typed. If you have a long
> argument list of, say, three positional arguments and ten optional keyword
> arguments, and you only want to annotate the first two positional arguments
> with types and leave the rest free, that's a lot nicer to express with two
> explicit type annotations than with grouped annotations and (potentially)
> explicit non-annotations.
>
I wonder whether you could include the colon but omit the type if it's 
the same
as that of the following parameter:

     def __init__(self,
             description:, sec_code:,
             vendor_name:, vendor_inv_num:,
             vendor_rtng:, vendor_acct:,
             transaction_code:, vendor_acct_type:str,
             amount:int, payment_date:Any)->None:



More information about the Python-ideas mailing list