[Python-ideas] Conventions for function annotations

Bruce Leban bruce at leapyear.org
Wed Dec 5 21:01:47 CET 2012


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]

--- Bruce
Follow me: http://www.twitter.com/Vroo http://www.vroospeak.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20121205/fae12413/attachment.html>


More information about the Python-ideas mailing list