你可以使用functools.partial:
>>> from functools import partial
>>> def foo(x,y):
... print x+y
...
>>> partial(foo,y=3)
>>> f = partial(foo,y=3)
>>> f(2)
5
在你的例子中:
def function(x,y):
pass # ...
r.sub(functools.partial(function,y=arg),string)
和正则表达式一起使用:
>>> s = "the quick brown fox jumps over the lazy dog"
>>> def capitalize_long(match,length):
... word = match.group(0)
... return word.capitalize() if len(word) > length else word
...
>>> r = re.compile('\w+')
>>> r.sub(partial(capitalize_long,length=3),s)
'the Quick Brown fox Jumps Over the Lazy dog'