by code_runner · 3y ago · 0 comments
Tried some poor-man’s debugging and never hit a breakpoint on the first significant line of code… took a while to figure out as it was my first Python project.
It almost feels like Python needs a scripting and non-scripting mode, or some kind of warning logging “you did everything wrong”
# jobs.py
@scheduler.register_job(when=“daily”)
def batch_job(): …
This adds the job to the global scheduler. # models.py
class Model(DBModel):
def on_change():
# do stuff
This adds the model to the ORM’s list of modules via a metaclasss and registers its hooks. # plugin.py
class SomePlugin(PluginBase): …
Same thing. The code to load plugins like this is just importing the module and letting the metaclass do the work.Our fix for this was to relegate stateful imports to files with "bootstrap" in the name, which the lazy loader stub would allow to be eagerly imported. Moreover, any imports listed in such "bootstrap" files would then be eagerly imported. But that's it (at least wrt the code belonging to our codebase); no one else, not even the children of the bootstrapping modules, were exempted from lazy loading.
This allowed for a centralization of all the stateful import effects. If you tried to write stateful code outside of bootstrapping, it simply wouldn't run. (You could hypothetically hack around this. But it would be rather obvious, and I'm perfectly ready to revert pull requests that try.)
Maybe you folks could try some variation of this in Python.
Although I found one feature to very strongly correlate -- static typing. For example, Julia is AOT compiled, but still dynamically typed. And coincidentally, it was designed for scientists, who need scripting much more.
Another observation is how easy it is to write unit-tests (for canonically written code). Overloading "import" statement doesn't help there at all.
Personally I am betting on JavaScript being the first dynamic and JITted (or incrementally compiled) language that can attain the AOT compilation without massively bloated binaries.