是否可以爲方法編寫包裝函數?是否可以將字符串方法作爲函數來處理?
>>> lowtide = [ 'oh', 'i', 'do', 'like', 'to', 'be', 'beside', 'the', 'seaside' ]
>>> [ x.capitalize() for x in lowtide ]
['Oh', 'I', 'Do', 'Like', 'To', 'Be', 'Beside', 'The', 'Seaside']
>>> list(map(lambda x: x.capitalize(), lowtide))
['Oh', 'I', 'Do', 'Like', 'To', 'Be', 'Beside', 'The', 'Seaside']
>>> def mef(m):
... def _mef(m,x):
... return x.m()
... return partial(_mef, m)
...
>>> list(map(mef(capitalize), lowtide))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'capitalize' is not defined
你的'mef'函數可以通過用'getattr(x,m)'替換'x.m'來工作。 – 2012-07-19 22:49:07