【发布时间】:2011-12-02 16:33:38
【问题描述】:
有人知道为什么os.path.join 函数不适用于str 的子类吗?
(我在Windows上使用的是Python3.2 x64和Python2.7 x86,结果是一样的)
这是我的代码
class Path(str):
def __add__(self, other):
return Path(os.path.join(self, other))
p = Path(r'C:\the\path')
d = p + 'some_file.txt'
以及我想要的结果:
'C:\\the\\path\\some_file.txt'
但无论self 的值如何,输出都是\\some_file.txt。
我知道我可以做 str(self) 或将其存储为 self.path 并稍后使用,但为什么 os.join.path 不接受 str 子类也不引发错误(比如当你使用数字或任何非字符串类型)?
【问题讨论】:
标签: python string path python-3.x