2

我想用一个额外的构造函数断言子类型 apathlib.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())。我在网上尝试了不同的建议解决方案,但没有任何进展。做什么?

4

1 回答 1

3

更新:经过一番挖掘后发现

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__成员。

于 2017-04-06T12:29:58.680 回答