import re
import string
def buildRule((pattern, search, replace)):
return lambda word: re.search(pattern, word) and re.sub(search, replace, word) 1
def plural(noun, language='en'): 2
lines = file('rules.%s' % language).readlines() 3
patterns = map(string.split, lines) 4
rules = map(buildRule, patterns) 5
for rule in rules:
result = rule(noun) 6
if result: return result
From : http://www.diveintopython.net/dynamic_functions/stage5.html
The above code works as expected. But buildRule
expects tuple but is passed a list in line annotated as 5, above.
Does python does the conversion automatically? If yes, then what is the general rule?
I searched the web, but couldn't get anything useful.