4 ms·
I used to love Python's dynamic typing, and magic, until I started working on build process critical Python written by other people - and it wasn't even a large
by EdwardDiego 5y ago
I used to love Python's dynamic typing, and magic, until I started working on build process critical Python written by other people - and it wasn't even a large codebase, it was about 1500 lines across five files.
My least favourite part - dicts of dicts of dicts parsed from JSON, so good luck knowing what conf["foo"]["bla"]["bob"] was without a lot of code reading, good luck figuring out what other properties conf["foo"]["bla"] had, and good luck figuring out if those properties always existed.
In the end I introduced Pydantic to give the JSON objects a known structure with validation, and that dramatically eased the maintenance friction in one foul swoop (and bringing in Pydantic exposed properties that everyone was setting religiously but never using) - instead of a dict of dicts, it was a now a dict of app name to BuildConfig.
Bringing in mypy and gradually introducing type annotations took more effort, but once it was done, the codebase was far easier to read and understand, large sections of dead code were easily identified, and runtime bugs became far less common.
In other words, I removed all the dynamic bits of Python.
That said, when I'm working with Python libs that do terrible things (like deciding if they're running in a multiprocessing context by checking for an ENV var when the module is imported(!)) I do appreciate how Python's magic lets me reach in and monkey patch terrible things away - although I feel a bit icky doing so.
- gary_0 5y agoI spend a lot of time in crufty old C++, and I love how clean Python syntax looks. But then I wonder if it's trying to be too clean, with all those pesky explicit types scrubbed away. Which is odd, because the first time I read "explicit is better than implicit" was in "The Zen of Python", and I always very much agreed.
- EdwardDiego 5y agoPython these days is sprouting type annotations all over the show, the benefits are too good to ignore. And if a given library doesn't use type annotations, people will often create type stubs for it that mypy can consume. https://github.com/python/typeshed/tree/master/stubs https://github.com/python/typeshed/tree/master/stubs TBH, Python generics are rather clunky, although I think they're probably still going to have cleaner type signatures than C++
- eyelidlessness 5y agoThis seems to be the story for many dynamic scripting languages. TypeScript obviously, but also Ruby and PHP. Lisp folks seem to be the most steadfast in preferring dynamic typing. But even that’s not universal.