[Python-ideas] PEP-3150
dag.odenhall at gmail.com
dag.odenhall at gmail.com
Thu Apr 14 18:53:15 CEST 2011
On 14 April 2011 18:34, Arnaud Delobelle <arnodel at gmail.com> wrote:
>
> On 14 Apr 2011, at 16:00, dag.odenhall at gmail.com wrote:
>
>>
>> orderly = sorted(disorderly, key=getscore) given def getscore(item):
>> return item.score
>
> To me, this is not as readable as:
>
> orderly = sorted(disorderly, key=getscore) given:
> def getscore(item):
> return item.score
I agree, I disliked the idea myself. There might be situations where
it'd be handy but I'm not convinced. If you need it because the code
is already indented several levels, it is likely that you won't fit
the "given def" on the same line anyway. If the function body is just
one statement you can inline it:
orderly = sorted(disorderly, key=getscore) given:
def getscore(item): return item.score
If the function is larger and you're already deeply indented you
should probably refactor the code anyway.
>
> Also, the related following is a good argument IMHO to allow expression statements before "given"
>
> mylist.sort(key=getscore) given:
> def getscore(item):
> return item.score
>
> If they were disallowed, I bet we would get "idioms" like:
>
> _ = mylist.sort(key=getscore) given:
> def getscore(item):
> return item.score
Certainly agree - I didn't know it was proposed to limit it to assignment?
More information about the Python-ideas
mailing list