4 ms·
You can do that in Python as well though. from typing import Union class Dog: dogtag: str name: str class Cat: name: str a
by mijamo 7y ago
You can do that in Python as well though.
from typing import Union
class Dog:
dogtag: str
name: str
class Cat:
name: str
allowedOutside: bool
Animal = Union[Cat, Dog]
def do(a: Animal):
print(a.name) # Always allowed
print(a.dogtag) # MYPY error
if isinstance(a, Dog):
print(a.dogtag) # MYPY allows
- sametmax 7y agoAnd you can even do that with dict in the most recent version of mypy. But the fact you have to import Union instead of using a pipe bothers me. It's useless boilerplate and verbose. So not o Python. I reported it, but Guido told me it will not happen.
- pas 7y agoExplicit is good in Python world. And I have to agree with him. Just look at the mess sclalaz did/does with its use of clever pictogram-like symbol names. Python is very much a language where newcomers are the norm. Make the source code at least a bit self-teaching seems like a good feature.
- sametmax 7y agoYou are right. Let's remove the "+" operator in favor of operator.add. It's totally worth the readability. I'm sorry but "|" is not only a very commonly known operator in the entire programming world, it is already an officially supported operator by python, vastly used by the community for doing unions: sets use it, sqlalchemy use it, pandas use it. Let's not confuse explicite and paperwork.