我想用一个额外的构造函数断言子类型pathlib.Path,如下所示
import pathlib
class TempDirPath(type(pathlib.Path())):
def __init__(self, path):
assert not os.path.isabs(path), "Temporary directory path must be relative"
super(TempDirPath, self).__init__(path)
但是这个错误是
TypeError: object.__init__() takes no parameters
为什么 super(TempDirPath, self) 评估为 object。不应该是 type(pathlib.Path())。我在网上尝试了不同的建议解决方案,但没有任何进展。做什么?
请您参考如下方法:
更新:经过一些挖掘后发现
class TempDirPath(type(pathlib.Path())):
def __init__(self, path):
assert not self.is_absolute(), "Temporary directory path '{}' must be relative".format(self)
解决它是因为 Path 是在 __new__ 成员中初始化的。它没有 __init__ 成员。
