[issue24132] Direct sub-classing of pathlib.Path

Kevin Norris report at bugs.python.org
Wed Jul 1 02:27:56 CEST 2015


Kevin Norris added the comment:

If I were designing pathlib from scratch, I would not have a separate Path class.  I would instead do something like this:

In pathlib.py:

    if os.name == 'nt':
        Path = WindowsPath
    else:
        Path = PosixPath

Alternatively, Path() could be a factory function that picks one of those classes at runtime.

Of course, that still leaves the issue of where to put the method implementations which currently live in Path.  We could change the name of Path to _Path and use the code above to continue providing a Path alias, but that might be too confusing.  Another possibility is to pull those methods out into top-level functions and then alias them into methods in WindowsPath and PosixPath (perhaps using a decorator-like-thing to pass the flavor, instead of attaching it to the class).

The main thing, though, is that Path should not depend on its subclasses.  That really strikes me as poor design, since it produces issues like this one.

----------
nosy: +Kevin.Norris

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24132>
_______________________________________


More information about the Python-bugs-list mailing list