search

Home  >  Q&A  >  body text

python - 关于re模块源代码

1.最近需要查看一下re模块的实现代码(不知道是不是python实现的,但是发现lib下面有一个re.py),
发现一个比较奇怪的问题,例如search功能:

def search(pattern, string, flags=0):
    """Scan through string looking for a match to the pattern, returning
    a match object, or None if no match was found."""
    return _compile(pattern, flags).search(string)

注意到search其实return了一个_compile(pattern, flags).search(string),就比较迷糊了:
a)这种形式算不算递归?但是search代码里面什么都没有写,怎么递归?
b)发现_compile(pattern, flags)其实是一个函数:

def _compile(pattern, flags):
后面省略

函数还能这样写function.function?之前只见过class.function用法,从没有见过一个函数使用“.”的方法?
不知道是我理解有误还是什么其他原因,烦请大神指导?
谢谢了!

阿神阿神2889 days ago385

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 17:46:13

    Look carefully at the implementation of the _compile method
    p = sre_compile.compile(pattern, flags)

    The .serarh you mentioned is actually the method below sre_compile.compile(pattern, flags)

    reply
    0
  • Cancelreply