[Python-ideas] Adding "Typed" collections/iterators to Python

Nick Coghlan ncoghlan at gmail.com
Mon Dec 19 05:29:59 CET 2011


On Mon, Dec 19, 2011 at 12:24 PM, Nathan Rice
<nathan.alexander.rice at gmail.com> wrote:
> On Sun, Dec 18, 2011 at 6:45 PM, Nick Coghlan <ncoghlan at gmail.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().

Still:

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.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list