On Sat, Mar 14, 2020 at 12:24 AM Noah Peter May <firstname.lastname@example.org> wrote:
> Hello everyone,
> I'm not certain if this has been brought up before, but I felt like bringing it to the table anyway.
> As the title says, it would be convenient to represent a union type with the or operator.
Yep! You're not the only one to want this.
> The next is actually a question: do we use "bitwise or" or "boolean or" (aka "|" vs "or")? I honestly don't know which would be preferable. The latter is more readable than the other but requires 3-4 characters (including whitespace) vs just 1-3 extra. Both are shorter than the 8-9 chars for the traditional union. I'll just use both in the examples for now.
The "or" operator is defined very thoroughly by the language and can't
be used for this, so the decision is made for you already - the "|"
operator is the only way to go.
> In either case, we could override the or-dunder method to return the "old" union representation of types
> List | Tuple == Union[List, Tuple]
> (Union[List, Tuple] or Union[int, str]) == Union[List, Tuple, int, str]
> This should allow any code relying on run-time annotations to work as expected.
Have a look at PEP 604, which is looking at this same idea.
I'm personally in favour :)
Python-ideas mailing list -- email@example.com
To unsubscribe send an email to firstname.lastname@example.org
Message archived at https://email@example.com/message/WQ2TI5YV5RNTCOGBS4ZVPHZEARXDLLJI/
Code of Conduct: http://python.org/psf/codeofconduct/