gpt4 book ai didi

python - pathlib.Path 子类型的构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 05:09:58 26 4
gpt4 key购买 nike

我想用一个额外的构造函数断言子类型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__ 成员。

关于python - pathlib.Path 子类型的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43250428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com