如下代码所示,add_age需要传递一个match对象,但是作为参数传入sub时,add_age没有参数。
str1 = 'I am 23 years old'
def add_age(match):
age = int(match.group())
age = age + 1
return str(age)
new_age = re.sub(r'\d+',add_age,str1)
print(new_age)
看过文档,If it is a callable, it's passed the match object and must return a replacement string to be used
看样子是已经将match传入了,就想知道这一步是如何实现的,原谅我不会去看源代码!!!
这是一个callbak