3 ms·
i'd honestly ask, why not? when you paste this code, it's infinitely more readable out of its original context with those hints.
by ledauphin 6y ago
i'd honestly ask, why not?
when you paste this code, it's infinitely more readable out of its original context with those hints.
- TameAntelope 6y agoI would argue it's less readable, substantially. Type hinting was never about readability, it was about getting IDEs/tools to do type checking for you. def max_width_of_vertical_area(points): xs = sorted(x for x, _ in points) return max(x2-x1 for x1, x2 in zip(xs, xs[1:])) Just seems easier...
- haukilup 6y agoAlthough I see both sides, I personally lean towards finding that type hints improve readability. And since I tend towards consistency, that ends with most/all functions being typed. Though I spend most my time writing Swift code, which definitely influences my preference.
- zo1 6y agoAnd then who-ever reads your code has no idea what "points" is. We had readable "type-annotation" 10+ years ago with comments to functions, this is just a logical extension to that. People need to know how to call your function, whether it's a statically-typed language or not. Would you say this is "less readable substantially" than your example? def max_width_of_vertical_area(points): # type points: List of List of int xs = sorted(x for x, _ in points) return max(x2-x1 for x1, x2 in zip(xs, xs[1:]))
- pansa2 6y agoI would never comment that such a function takes a `List of List of Int` - I would instead describe `points` as being something like an `Iterable of Pairs of Numbers`. Are Python type hints able to specify types like that?
- zo1 6y agoThe new type-hints sure: List[List[int]] (and you can do this to any arbitrary types or levels of nesting. See fancy example at the end.). But older iterations before the formal type-hint spec had varied/non-standard ways of doing it. Some machine-readable but others more for the user (like List of List of int). Even once the type-hint spec got approved, it was backported to older versions of python using docstrings. My experience even before that was mostly with PyDev in eclipse. The trick back then was identifying good spots to add type-hints, afterwhich the "IDE" would take over and propagate them through the different variables and calls using static code analysis. It was surprisingly good and helped immensely with very few actual instances of types being hinted. Another one was PyCharm that would "save" or "cache" the types during debug/test runs and use them as type-hints without you explicitly having to define them. Fancy example: List[Dict[str, MyCustomTypeGeneric[str]]] Which gives you a list of dictionaries with keys of type string, holding values MyCustomTypeGeneric with generic type str.
- dragonwriter 6y ago> I would instead describe `points` as being something like an `Iterable of Pairs of Numbers`. Yes, Iterable[Tuple[Number, Number]] is a valid type, though 2D points are probably Tuple[Real, Real] or just Complex, not Tuple[Number, Number].
- TameAntelope 6y agoPeople? It’s a leetcode problem...
- deleted 6y ago[deleted]