1

In C++ codebases you tend to extract out certain commonly used small functions to a utils.h header file for reusability and to avoid defining them as methods when they are not class methods per se.

What would be the equivalent practice in Python? Just create a utils.py and import that?

A. Fenzry
  • 414
  • 3
  • 12
  • You can try going around python repositories in github to see if there is an equivalent practice. I'm sure you can just do what you suggest. As to whether or not it is best practice, browsing popular repositories in python would be a way to answer that for yourself. – ferreiradev Nov 30 '22 at 03:17
  • Sounds reasonable. I would add that in large projects, a "utils" source file tends to become a giant dumping ground full of all kinds of random crap, half of which has been superceded by something else undocumented, or only used in one highly fragile legacy system, or has been duplicated and/or reimplemented in other parts of the codebase with minor variations or task-specific optimizations... Then when you want to add a utility, it's hard to even find the best place to add it. You always think "I should refactor this damn thing", but you never do. – paddy Nov 30 '22 at 03:22
  • To avoid that, one can evaluate carefully: is this `format_with_color(str, color)` function *truly a general purpose utility meant to exist in utils.py* or should I treat it as part of a proper module such as `CustomFormatters` or similar? – ferreiradev Nov 30 '22 at 03:31
  • Does this answer your question? [If a "Utilities" class is evil, where do I put my generic code?](https://stackoverflow.com/questions/3339929/if-a-utilities-class-is-evil-where-do-i-put-my-generic-code) – ferreiradev Nov 30 '22 at 03:36
  • Thanks. While helpful, I'm not looking for an alternative to a utils file but for the pythonic approach to it in most codebases. The part of making it a module is good feedback too – A. Fenzry Nov 30 '22 at 10:36

0 Answers0