[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484

Ethan Furman ethan at stoneleaf.us
Mon Aug 1 19:41:52 EDT 2016


On 08/01/2016 04:31 PM, Guido van Rossum wrote:
> On Mon, Aug 1, 2016 at 2:46 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 08/01/2016 02:31 PM, Guido van Rossum wrote:
>>
>>> Third, there's an annoying thing with tuples/commas here. On the one
>>> hand, in a function declaration, we may see (a: int = 0, b: str = '').
>>> On the other hand, in an assignment, we may see
>>>
>>> a, b = 0, ''
>>>
>>> Suppose we wanted to add types to the latter. Would we write this as
>>>
>>> a: int, b: str = 0, ''
>>
>>
>> If keeping it all on one line, I find this far more readable:
>>
>> - it keeps the type right next the name (imagine if there five names and
>> types)
>> - it mirrors the function header style (one less thing to remember)
>
> But what would this do?
>
> a: int, b: str = x
>
> Does the x get distributed over (a, b) or does a remain unset? The
> analogy with assignment suggest that x gets distributed, but the
> analogy with function definitions says x only goes to b.

When speaking of the function header I meant that we already have one way to say
which name has which type, and it would be simpler (at least to understand) if
we stick with one way to specify type.

As far as questions such as "what would this do" I would say it should do the
exact same thing as if the type info wasn't there:

   a: int, b: str = x   simplifies to

   a, b = x

and so x had better be a two-item iterable;  on the other hand:

   def func(a: int, b: str = x):  simplifies to

   def func(a, b=x):

and so parameter a is mandatory while parameter b has a default and so is
optional.

--
~Ethan~


More information about the Python-ideas mailing list