3 ms·
It seems odder to me that 42 is a valid type hint than that the LHS can be an expression. But I share with most of the others here the opinion that this really
by msluyter 8y ago
It seems odder to me that 42 is a valid type hint than that the LHS can be an expression. But I share with most of the others here the opinion that this really isn’t a problem with Python. Every language makes trade offs, and python gives you incredible flexibility and expressiveness in exchange for a lack of certain guardrails.
Once a coworker came to me with a program that was unexpectedly failing, and it turned out that somewhere in the code there was a function that takes a callable, but instead of
something(iterable, func)
he had written
something(iterable, func())
There were no IDE errors/warning (in fact PyCharm tends to induce this error by auto inserting the parens) and the behavior that resulted made it tricky to catch.
Nonetheless, personally I’d take that over most other options for run of the mill progamming tasks.
- heavenlyblue 8y agoPyCharm would highlight that error as a yellow warning if you correctly define the types of the arguments for "something". This would also not be accepted by mypy.
- msluyter 8y agoIirc this was pre type hints, so the example might be invalid now, but I’m sure I could come up with other examples that demonstrate the same point.