3 ms·
> [pathlib] is a good object-oriented solution I'd agree that pathlib is a good solution, and that it's an object-oriented solution, but I'm hesitant to call i
by WillDaSilva 5y ago
> [pathlib] is a good object-oriented solution
I'd agree that pathlib is a good solution, and that it's an object-oriented solution, but I'm hesitant to call it a good object-oriented solution. It uses some object-oriented hackery to work as elegantly as it does. I approve of this decision, but it leads to some weirdness such as the inability to subclass `pathlib.Path`. Instead you need to subclass `type(pathlib.Path())`, as the method resolution order changes once it is instantiated:
>>> from pathlib import Path
>>> Path.__mro__
(<class 'pathlib.Path'>, <class 'pathlib.PurePath'>, <class 'object'>)
>>> type(Path()).__mro__
(<class 'pathlib.PosixPath'>, <class 'pathlib.Path'>, <class 'pathlib.PurePosixPath'>, <class 'pathlib.PurePath'>, <class 'object'>)
In any case, weirdness like this can make for an even better learning opportunity!
- hexane360 5y agoI was bitten by something related to this a couple weeks ago: >>> with open("pickle.pkl", "rb") as f: ... pickle.load(f) Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/lib/python3.9/pathlib.py", line 1074, in __new__ raise NotImplementedError("cannot instantiate %r on your system" NotImplementedError: cannot instantiate 'WindowsPath' on your system