2 ms·
I prefer putting the main code into a "main" function (called from the __name__ == '__main__' block) fairly early, since otherwise the functions you extract mig
by ptx 7y ago
I prefer putting the main code into a "main" function (called from the __name__ == '__main__' block) fairly early, since otherwise the functions you extract might accidentally keep relying on global variables.
- tony 7y agoGood point I like to do it early also, to make sure that the new script, if imported from by a sibling module, is inert. An example would be a scripts/ folder and sharing a few functions between scripts w/o duplicating. In some cases I don't have a choice. Initialization of a flask app/ORM stuff/etc has to be done in the correct order. I think the general rule of thumb I follow is: avoiding keeping code that'd "run" in the root level. Keep it in blocks (normally to me functions) has the added effect of labeling what is does. What I don't do: I don't introduce classes until very late. In hindsight, every time I tried to introduce a complicated object model, I feel I tended to overengineer / encounter YAGNI