On Mon, Dec 19, 2011 at 12:24 PM, Nathan Rice firstname.lastname@example.org wrote:
On Sun, Dec 18, 2011 at 6:45 PM, Nick Coghlan email@example.com wrote:
Another bad example, since that's just a really verbose way of writing my_string.capitalize().
The python interpreter says otherwise...
foo = "line 1\nline 2\nline 3" foo.capitalize()
'Line 1\nline 2\nline 3'
"\n".join(s.capitalize() for s in foo.split("\n"))
'Line 1\nLine 2\nLine 3'
Ah, my mistake, I was thinking of title() rather than capitalize().
def capitalize_lines(s): return "\n".join(s.capitalize() for line in s.split("\n"))
There comes a time when the contortions people go to to avoid naming a frequently repeated operation just get silly. If you do something a lot, pull it out into a function and name it. The named function can even be a local closure if the usage is sufficiently specific to one operation - then it can still reference local variables without requiring a lot of additional parameters.