如何将函数应用于某个对象?

2024-04-26 04:37:57 发布

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

我想在“re”python模块中创建一个对方法起作用的方法,如下所示:

_compile(pattern, flags).finditer(string)

函数查找器正在编译中应用,如何使用?\u compile的返回类型是string吗?如果是这样的话,如何创建应用于字符串的方法呢


Tags: 模块方法函数字符串re类型stringflags
1条回答
网友
1楼 · 发布于 2024-04-26 04:37:57

如果您创建了一个具有实例方法finditer的类,并在_compile方法中返回它,那么您就可以使用它。这基本上就是re所做的

在不知道你想做什么的情况下,我会告诉你我的意思:

class CompiledClass:

    def __init__(self, pattern, flags):
        #setup class here

    def finditer(self, s):
        #perform the find or whatever it is you’re doing on the string and yield each result or return a list.

def _compile(pattern, flags):
    #do any checks or setup before creating/returning the class
    return CompiledClass(pattern, flags)

相关问题 更多 >