What is your favourite package in the Python standard library and why?
And least favourite?
I really like getpass
and dataclasses
myself. dataclasses
makes class creation so much quicker and easier whilst enforcing type hinting in an easy-to-understand way. It's just a really nice addition to the language. Although getpass
is great too, because it does one small thing very well.
If you don't know dataclasses
look like this:
Regular class:
class Car: def __init__(self, make: str, model: str): self.model = model self.make = make
But the same as a dataclass is just
from dataclasses import dataclass @dataclass class Car: make: str model: str
And they both work exactly the same! It makes writing classes so much easier, especially when teaching new people about classes.
Comments
-
My favourite has to be
Pathlib
, it's a great example of a package that takes a tedious and error-prone task and makes it much simpler and more reliable.Using the package is as simple as creating a
Path
object from a string, python will then handle path separators and globs for you correctly regardless of your platform. This allows you to write much clearer code that's inherently cross-platform.My least favourite is probably
gettext
, this is the stdlib interface for internationalization and localization. It's a wrapper for a core utility on linux also calledgettext
, which allows applications to display text in different languages for different users. Unfortunately there are some existing bugs in the supporting tools, and neither the file format nor the packaging workflow are very pythonic.2 -
+1 for
pathlib
anddataclasses
. I use them all the time and this makes life so much easier for manipulating paths and data (respectively).I would also add the
logging
module, which is a bit hard to master, but improve the quality of code to control how messages are displayed.And of course
re
when you want to use regular expressions.1 -
Massive +1 for logging!
I also quite like the JSON reader/writer - dead handy for passing data.
1 -
Ooo Logging is v powerful, plus we could really do with a guide to logging... When I get some time I might write an introductory article about it. Same for JSON.
0