On Fri, Aug 30, 2019 at 1:17 AM Philippe Prados
<philippe.pra...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RJ55YRRRV3EDYR47TL3YQ4FD6Q27ZYPQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to