I would like to enhance the class pathlib.Path
but the simple example above dose not work.
from pathlib import Path
class PPath(Path):
def
Here is the definition of the Path
class. It does something rather clever. Rather than directly returning an instance of Path
from its __new__()
, it returns an instance of a subclass, but only if it's been invoked directly as Path()
(and not as a subclass).
Otherwise, it expects to have been invoked via either WindowsPath()
or PosixPath()
, which both provide a _flavour
class attribute via multiple inheritance. You must also provide this attribute when subclassing. You'll probably need to instantiate and/or subclass the _Flavour class to do this. This is not a supported part of the API, so your code might break in a future version of Python.
TL;DR: This idea is fraught with peril, and I fear that my answers to your questions will be interpreted as approval rather than reluctant assistance.