[Python-ideas] Conventions for function annotations

Masklinn masklinn at masklinn.net
Thu Dec 6 09:43:34 CET 2012


On 2012-12-06, at 00:06 , Guido van Rossum 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).
>> 
>> Well if `|` is the "union operator", as Ben notes `T | None` works well,
>> is clear and is sufficient. Though that's if and only if "Optional[T]"
>> is equivalent to "T or None" which Bruce seems to disagree with. There's
>> some history with this pattern:
>> http://journal.stuffwithstuff.com/2010/08/23/void-null-maybe-and-nothing/
>> (bottom section, from "Or Some Other Solution")
> 
> Actually, I find "T|None" somewhat impure, since None is not a type
> but a value. If you were allow this, what about "T|False"? And then
> what about "True|None"? (There's no way to make the latter work!) And
> I think "T|NoneType" is obscure; hence my proposal of Optional(T).
> (Not Optional[T], since Optional is not a type.)

Why would Optional not be a type? It's coherent with Option or Maybe types
in languages with such features, or C#'s Nullable.



More information about the Python-ideas mailing list