CPython中的字符串'in'运算符

阿梅里奥·巴斯克斯·雷纳(Amelio Vazquez-Reina)

据我了解,当我'foo' in 'abcfoo'使用Python进行操作时,解释器会尝试在后台进行调用'abcfoo'.__contains__('foo')

这是一个字符串匹配(aka搜索)操作,它接受多种算法,例如:

在此处输入图片说明

我怎么知道给定实现可能使用哪种算法?(例如,带有CPython的Python 3.8 )。我无法查看这些信息,例如CPython的字符串源代码我不熟悉其代码库,例如找不到该库的__contains__定义。

伊万·C

根据源代码

/* 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 */

该链接表明已在中使用它find, index, split, replace, __contains__,尽管它可能已经过时了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章