![](https://secure.gravatar.com/avatar/d67ab5d94c2fed8ab6b727b62dc1b213.jpg?s=120&d=mm&r=g)
On Fri, Aug 30, 2019 at 1:17 AM Philippe Prados <philippe.prados@gmail.com> wrote:
I propose to use a unary operator to help the readability. With a lot of parameters:
def f(source: str | None, destination: str | None, param: int | None):...
I think it's more readable with
def f(source: str?, destination: str?, param: int?): ...
TBH I don't see much of an advantage here - not enough to justify the creation of an entire new operator. "int | None" is already far from terrible, and the slight abuse of "~int" meaning "maybe int" is pretty plausible (consider how "approximately equal" is written mathematically). BTW, is there a strong reason for these union types to be disallowed in instance/subclass checks?
isinstance(3, Union[str, int]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.9/typing.py", line 764, in __instancecheck__ return self.__subclasscheck__(type(obj)) File "/usr/local/lib/python3.9/typing.py", line 772, in __subclasscheck__ raise TypeError("Subscripted generics cannot be used with" TypeError: Subscripted generics cannot be used with class and instance checks
If they were permitted, then instance checks could use an extremely clean-looking notation for "any of these": isinstance(x, str | int) ==> "is x an instance of str or int" It's very common for novices to write "if x == 3 or 5:", and I'm not sure whether that's an argument in favour or against. ChrisA