As above. I made this attempt below but it throws an error:
I don't know how to mutate self
to get what I want.
from pathlib import Path
class P(type(Path()), Path):
def __setitem__(self, key, value):
fullparts = list(self.parts)
fullparts[key] = value
return P(*fullparts)
def __getitem__(self, key):
return P(self.parts[key])
p = P("folder/subfolder/file.txt")
p[1] = "haha" #
Desired outcome folder/haha/file.txt
.
Actual outcome: object not changed!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…