[Python-ideas] Conventions for function annotations

Guido van Rossum guido at python.org
Thu Dec 6 00:01:16 CET 2012


On Wed, Dec 5, 2012 at 12:01 PM, Bruce Leban <bruce at leapyear.org> wrote:
>
>
> On Wed, Dec 5, 2012 at 11:22 AM, Guido van Rossum <guido at python.org> wrote:
>>
>> - Unions. We need a way to say "either X or Y". Given that we're
>> defining our own objects we may actually be able to get away with
>> writing e.g. "Int | Str" or "Str | List[Str]", and isinstance() would
>> still work. It would also be useful to have a shorthand for "either T
>> or None", written as Optional[T] or Optional(T).
>
>
> Optional is not the same as "or None" to me:
>
> Dict(a=Int, b=Int | None, c=Optional(Int))
>
>
> suggests that b is required but might be None while c is not required, i.e.,
> {'a': 3, b: None} is allowed while {'a': 3, c: None} is not.
>
> Ditto for Tuples:
>
> Tuple[Int, Str | None, Optional(Int)]
>
> where (3, None) matches as does (3, 'a', 4) but not (3, None, None).
>
> Optionals might be restricted to the end as matching in the middle would be
> complicated and possibly error-prone:
>
> Tuple[Int, Optional(Int | None), Int | Str, Int | None]

Those are not the semantics I had in mind for Optional.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list