When someone asks why the if __name__ == '__main__' idiom should be used, I
say it's because it makes turning your "script" into a "library" a seamless
thing. Novices often write a series of one-off Python scripts that exist only as
long as it takes to finish and run them. More seasoned developers have accumulated a set of libraries they've written over
the years.
Last night I needed to properly parse a fully-qualified domain into
its constituent parts (top-level domain, second-level domain, sub-domains). I
found one library that looked promising, but it used a hard-coded list to
represent all of the possible TLDs. The problem is this list changes with some
frequency, and I needed to get it right.
So, fine, there's no third-party package that exactly fits my needs. Rather than
just bolt on the domain parsing functionality to the application that required
it, however, I turned it into a first-class citizen: a library (more accurately,
a Python package). Heck, I even released it with unit tests and hooked it up to
TravisCI.
Why would I take the extra time required to make this a reusable library? Two
main reasons: first, this is a straightforward problem that should be solved once and never
thought of again. Any time you encounter .. cntd
↧