3 ms·
Python 3 supports typing. The integration with VSCode is also nice. The following is valid typed code and my personal favorite WTF: def func() -> int:
by rvanlaar 7y ago
Python 3 supports typing. The integration with VSCode is also nice.
The following is valid typed code and my personal favorite WTF:
def func() -> int:
return True
Because: https://github.com/satwikkansal/wtfpython/blob/master/README.md#-whats-wrong-with-booleans https://github.com/satwikkansal/wtfpython/blob/master/README...
I understand the decision that was made in the python 2 era.
Nowadays Boolean shouldn't be a subtype of `int`.
It goes against the zen of python:
- Explicit is better than implicit.
It actually happened because I was using the new walrus operator (which I like).
def calc(input: int) -> int:
return input // 3
def func(input: int) -> Iterator[int]:
while input:= calc(input) > 0:
yield input
*edit: code layout
- d33 7y agoYeah, typing KINDA works until you try to use a library that has no annotations and uses reflections, such as - say - plumbum, boto3 or some other pre-typing shit. It's a bit like with async code - once you decide to use it, you can keep the language but basically need a whole new ecosystem. I tried to write an actual company project with `mypy --strict` passing as a requirement and you quickly end up having abstractions only to bypass mypy, as well as surpress comments as uses of Any. And this is where it pays off to just switch to a statically typedk, compiled language.
- rvanlaar 7y agoYeah, agreeing with what you say. mypy --strict is only useful for projects for which everything is defined. I recently did a project with tornado, which is typed. Typing helped me immensely. My editor would give me type hints, errors and show me other edge cases. For an existing Django project I'm adding types when I touch functions. It helps a bit, but way less. That's also because typing almost forces you to change all the dicts that get's passed around in dataclasses. Which is a lot of work after in an existing project.
- 333c 7y agoFor what it's worth, I like that a bool is an int. It enables concise constructions such as sum(is_true(data) for data in items) to count how many items in an iterable satisfy a condition.
- woogley 7y agoJust for fun, I thought of a way to do this if booleans didn't coerce: sum(1 for data in items if is_true(data))