I\'m trying to sublclass Path from pathlib, but I failed with following error at instantiation
from pathlib import Path
class Pl(Path):
def __init__(self
Path
is somewhat of an abstract class that is actually instantiated as on of two possible subclasses depending on the OS:
PosixPath
or WindowsPath
https://docs.python.org/3/library/pathlib.html
This base class looks for an internal (private) class variable to determine what type of object it actually is, and this variable is called _flavour
You have two choices:
The code will look like this:
import os
if os.name == 'posix':
base = PosixPath
else:
base = WindowsPath
class P1(base):
def __init__(self, *pathsegments: str):
super().__init__(*pathsegments)
Path
directly.Note, there may be other issues if you decide to use this method!
import os
from pathlib import _PosixFlavour
from pathlib import _WindowsFlavour
class P1(Path):
_flavour = _PosixFlavour() if os.name == 'posix' else _WindowsFlavour()
def __init__(self, *pathsegments: str):
super().__init__(*pathsegments)