Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or str, or in the other example str or NoneType).
Python deals in objects only, so there is never a need to even consider 'union types'. Python functions return what they return, if the programmer wants to return different types for different results then that's their choice. The choice is then an architecture choice, and makes no difference to the Python interpreter (so there is nothing to 'benchmark' here).
Python 3.5 does introduce a standard for creating optional type hints, and that standard includes Union[...] and Optional[...] annotations. Type hinting adds optional static type checking outside of the runtime, the same way types in TypeScript are not part of the JavaScript runtime.
Union typing is only needed when you have a statically typed language, as you need to declare that an object can return one of multiple types (in your case an int or str, or in the other example str or NoneType).
Python deals in objects only, so there is never a need to even consider 'union types'. Python functions return what they return, if the programmer wants to return different types for different results then that's their choice. The choice is then an architecture choice, and makes no difference to the Python interpreter (so there is nothing to 'benchmark' here).
Python 3.5 does introduce a standard for creating optional type hints, and that standard includes Union[...] and Optional[...] annotations. Type hinting adds optional static type checking outside of the runtime, the same way types in TypeScript are not part of the JavaScript runtime.
the type itself does not exist because Python is just a dynamically typed language, however, in newer Python versions, Union Type is an option for Type Hinting,
from typing import Union,TypeVar
T = TypeVar('T')
def f(x: T) -> Union[str, None]:
if x:
return "x"
you can use that to annotate your code, thus enabling IDE/Editor level syntax checking.
Videos
If you import
from __future__ import annotations
you can use the union types with | syntax starting from Python 3.7, without errors.
$ python3.7 -c 'from __future__ import annotations; a: int | str = 42'
If you don't use the __future__ import to enable new PEP 563 behavior, Python evaluates the annotations at runtime and you get an exception because prior to 3.10, types don't define the | operator.
When using __future__ import, you need to rely on your IDE or running mypy to catch incorrectly formatted annotations. Use sufficiently new mypy that supports this new syntax.
(For reference, here's what happens without the __future__ import.)
$ python3.7 -c 'a: int | str = 42'
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'type'
PEP 604, which was implemented in Python 3.10 allows Union types to be formed using the | operator (which has existed with exclusively numeric meanings for most of Python's history).
So in sufficiently recent versions of Python, you can write your type hint like this, with no imports required from typing:
some_stuff: list[int|str|float|list[str]] = [98, "Fido", -34.925, ["Phantom", "Tollbooth"]]
While that specifically answers your question, I'd note that it's a little suspicious that your example data exactly matches the order of the types you're listing in the Union. If you are always going to have exactly one of each kind of data (rather than some unknown mixture of them), you probably want to be type hinting the value differently, e.g. with tuple[int, str, float, list[str]] which specifies that there will be one value of each, in that order. You might need to use an actual tuple for the value, I'm not sure there's a way to hint a fixed pattern of values within a list object.