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

Steven D'Aprano steve at pearwood.info
Sun Aug 17 10:34:53 CEST 2014


On Sun, Aug 17, 2014 at 08:41:33AM +0100, Nicholas Cole wrote:
> On Sun, Aug 17, 2014 at 3:08 AM, Steven D'Aprano <steve at pearwood.info> wrote:
[...]
> >     def __init__(self,
> >             description:Str10, sec_code:SecurityCode,
> >             vendor_name:Str22, vendor_inv_num:Str15,
> >             vendor_rtng:Str9, vendor_acct:Str17,
> >             transaction_code:ACH_ETC, vendor_acct_type:VendorAcctType,
> >             amount:Pennies, payment_date:DateABC)->None:
> 
> I know that the BDFL has spoken on this issue and said that he finds
> all of this readable and "pythonic", but these examples perfectly
> capture what I am going to dislike about this syntax as it becomes
> popular.

Even though I am in favour of the proposal, I do sympathise, and I see 
what you mean. But, I think it is important to realise that a method 
with ten arguments (plus self) is not going to be exactly readable at 
the best of times.


> I suppose it will be better when I am reading it in an editor that has
> syntax highlighting but as it stands I had to stare at that block of
> code for a long time to see how many and what type of arguments it
> called.  At first I thought you had one per line, then I thought you
> had variable numbers per line.  On about the fourth or fifth reading,
> I saw you had two per line.

Look for the commas :-)

But yes, as given that makes a big wall of text. I suppose it will take 
some time for people to decide what formatting works best for them. It 
might help to align the arguments in columns (even though that goes 
against PEP-8):

     def __init__(self,
            description:Str10,        sec_code:SecurityCode,
            vendor_name:Str22,        vendor_inv_num:Str15,
            vendor_rtng:Str9,         vendor_acct:Str17,
            transaction_code:ACH_ETC, vendor_acct_type:VendorAcctType,
            amount:Pennies,           payment_date:DateABC,
            ) -> None:

That works for me.


> My fear with all of this is that it turns python into a language that
> is harder for humans to read.

Declaring types in the function parameter list is very common, in many 
languages. If it were *that* much harder to read, languages wouldn't 
keep using it. (Not many languages follow Forth or APL syntax.) Perhaps 
because I learned to program in Pascal, I find the annotation syntax 
very easy to read, but, yes, anything which increases the density of 
information per line risks hurting readability a little.

It's a tradeoff, and of course all of this is optional. Syntax 
highlighting will help, and I expect that in a few years time emacs and 
vim will have some way to hide annotations when editing code :-)


-- 
Steven


More information about the Python-ideas mailing list