如何更改pathlib.Path._parse_args
,这样我就不能只使用其他类型(如LocalPath
)作为Path
的参数,还可以使用其他类型作为基于/
的连接的参数,等等。。?在
from pathlib import Path
Path(tmplib) / 25 / True
使用tmplib
aLocalPath
来自py._path.local
,其他的自动转换为它们的str()
表示?在
我尝试了像这个(pathlib Path and py.test LocalPath)问题所示的子分类和猴子修补,但没有奏效。在
Path
看起来很难扩展。在
如果您想保持平台独立的魔力,您可以这样做:
或者,如果可以针对特定平台:
^{pr2}$pathlib
(对于Python版本<;3.4,pathlib2
)主要由四个与路径Path
、PosixPath
、WindowsPath
和{BasePath
中的pathlib2
)组成。如果您将其中的每一个子类化,并按照以下方式复制并调整Path.__new__()
和PurePath._parse_args()
的代码:您将有一个
^{pr2}$Path
,它已经理解了int
,可以用来做:获得:
(您需要为版本<;3.4安装包
pathlib2
才能在这些版本上工作)。在上面的
Path
可以在python3.6中用作open(p)
。在调整
_parse_args
可以自动支持/
(__truediv__
),以及joinpath()
,relative_to()
等方法相关问题 更多 >
编程相关推荐