8

When using re.sub() part of re for python, a function can be used for sub if I am not mistaken. To my knowledge it passes in the match to whatever function is passed for example:

r = re.compile(r'([A-Za-z]')
r.sub(function,string)

Is there a smarter way to have it pass in a second arg other than with a lambda that calls a method?

r.sub(lambda x: function(x,arg),string)
Stoof
  • 687
  • 2
  • 6
  • 12

2 Answers2

11

You can use functools.partial:

>>> from functools import partial
>>> def foo(x, y):
...     print x+y
... 
>>> partial(foo, y=3)
<functools.partial object at 0xb7209f54>
>>> f = partial(foo, y=3)
>>> f(2)
5

In your example:

def function(x, y):
     pass # ...
r.sub(functools.partial(function, y=arg),string)

And a real use with regexes:

>>> 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'
brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • Ah thank you! Almost the same as lambda :] Would this be the more "pythonic" way of approaching it? – Stoof May 04 '12 at 18:49
  • @Stefan my sentiment is that most Python masters would consider partial more pythonic, although it can be rather subjective. – brandizzi May 04 '12 at 18:54
1

Without using partials:

import re

a = 'the quick brown fox jumped over the green lazy python'
print re.sub("(\w+)", 
  lambda x: x.group(1).capitalize(),
  a)

Lambda's in Python provide nicely for inline modification of groups.

Having come from Perl, this is how I think of it:

$a = 'the quick brown fox jumped over the green lazy python';
$a =~ s/(\w)(\w+)/uc($1).$2/ge;
print "$a\n";
KJ7LNW
  • 1,437
  • 5
  • 11