3 ms·
timeout = timedelta(seconds=300) frobnicate(timeout) Working with GCP or Azure's Python SDK is like navigating a jungle of types. Some calls return a `comp
by wodenokoto 5y ago
timeout = timedelta(seconds=300)
frobnicate(timeout)
Working with GCP or Azure's Python SDK is like navigating a jungle of types. Some calls return a `compute_engine_list_item` while others return a `compute_engine` type and these are difficult to inspect and reason about, because Python classes default to printing something along the lines of `<__main__.myclass at 0x7fa8864a1040>`, making heavily typed Python code quite difficult to work with.
No paradigm is going to save you from spaghetti-code, but being able to pass a list and get an integer in return, makes it very easy to reason about what you can do with these values, whereas it can be quite difficult to reason about custom types/objects.
My point is, that knowing if `frobnicate(timeout=300)` is in seconds or minutes can be just as difficult (or even more difficult) as knowing what specific object I need to instantiate and pass to `frobnicate` (in the above case a `timedelta`)
- Aeolun 5y agoI do not think the fact that Google API’s have terrible types is a good argument against typing in general.
- dharmab 5y agoWhen I dealt with the Azure SDk for Python I frequently wrote small scripts that created the types in question and then called breakpoint() so that I could examine the types interactively.
- akdor1154 5y agoIn general you're right.. strong typing needs good tooling and thourough language support to be fun. Python is a bit lacking in both of these. In this specific example i disagree, timedelta is part of the standard library and should be widely known and familiar.
- chriswarbo 5y ago> being able to pass a list and get an integer in return, makes it very easy to reason about what you can do with these values I disagree; to paraphrase Ian Malcolm: what you can do with these values is less important than what you should do with these values. For example, we can add a distance to a currency, if they're both int or float; that doesn't mean we should. The most obvious example of this is "stringly-typed programming", where pretty much everything is "string". Can I append a user-input string to an SQL statement string? Sure; but I shouldn't. Can I append a UserInput to an SQLStatement? Not without conversion (i.e. escaping)!
- necovek 5y agoBadly designed type systems should not be the measuring stick. But in your example above, typed Python would instruct your editor and tools like mypy to flag any improper use of frobnicate. You can set up your editor to offer you a tip on what type to use as soon as you type in `frobnicate(`. In cases where typing is not really available, I prefer to put types into APIs instead of variable names (and Python makes that great: `frobnicate(duration_as_timedelta=timeout)`).