内置连接函数使用什么方法?

2024-05-08 09:13:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用

a = ''.join((Symbol('a'), Symbol('b'))

但我明白了

File "/home/j/_Github-Projects/MiscScripts/string_permutations.py", line 72, in get_permutations
    permutation = Symbol(''.join(permutation))
TypeError: sequence item 0: expected str instance, Symbol found

python文档说iterable被接受为join的参数,但这似乎与异常告诉我的相反

我试着在Symbol中定义一些基类方法,但没用

def __concat__(self, other):
    return Symbol(self.symbol + other.symbol)
__add__ = __concat__
__and__ = __concat__

def __iconcat__(self, other):
    self.symbol += other.symbol
    return self

str.join(iterable) docs


Tags: selfgithubhomereturndefsymboliterablefile
2条回答

好的,应该这样做,而不是join

        permutation = functools.reduce(
            lambda x, y: x.concatenate(y),
            permutation)

动态语言的乐趣在于,只看一眼参数名,却没有意识到只接受某些值类型

这个doc还说:

A TypeError will be raised if there are any non-string values in iterable...

毕竟,Symbol('a')不是一个字符串

相关问题 更多 >

    热门问题