Python 中的字符串 'in' 运算符
The string 'in' operator in CPython
据我了解,当我在 Python 中执行 'foo' in 'abcfoo'
时,解释器会尝试在幕后调用 'abcfoo'.__contains__('foo')
。
这是一个 string matching(又名搜索)操作,它接受多种算法,例如:
我如何知道给定的实现可能使用了哪种算法? (例如 Python 3.8 和 CPython). I'm unable to this information looking at e.g. the source code for CPython for string。我不熟悉它的代码库,例如我找不到为它定义的 __contains__
.
根据 source code:
/* fast search/count implementation, based on a mix between boyer-
moore and horspool, with a few more bells and whistles on the top.
for some more background, see: http://effbot.org/zone/stringlib.htm */
link 表明它在 find, index, split, replace, __contains__
中使用,尽管它现在可能已经过时了。
据我了解,当我在 Python 中执行 'foo' in 'abcfoo'
时,解释器会尝试在幕后调用 'abcfoo'.__contains__('foo')
。
这是一个 string matching(又名搜索)操作,它接受多种算法,例如:
我如何知道给定的实现可能使用了哪种算法? (例如 Python 3.8 和 CPython). I'm unable to this information looking at e.g. the source code for CPython for string。我不熟悉它的代码库,例如我找不到为它定义的 __contains__
.
根据 source code:
/* fast search/count implementation, based on a mix between boyer-
moore and horspool, with a few more bells and whistles on the top.
for some more background, see: http://effbot.org/zone/stringlib.htm */
link 表明它在 find, index, split, replace, __contains__
中使用,尽管它现在可能已经过时了。