[Python-ideas] Long Live PEP 3150 (was: Re: Statement local functions and classes (aka PEP 3150 is dead, say 'Hi!' to PEP 403))
Antoine Pitrou
solipsis at pitrou.net
Mon Oct 17 14:30:38 CEST 2011
On Sun, 16 Oct 2011 17:16:40 +1000
Nick Coghlan <ncoghlan at gmail.com> wrote:
> # Embedded assignments in if statements
> if match is not None given match=re.search(pattern, text):
> # process match
> else:
> # handle case where regex is not None
>
> # Embedded assignments in while loops
> while match is not None given match=re.search(pattern, text):
> # process match
> else:
> # handle case where regex is not None
Urk. What is the use case for these? Save one line of code but actually
type *more* characters?
"given" doesn't look like a very pretty notation to me, honestly.
Apparently you're looking for a way to allow assignments in other
statements but without admitting it (because we don't want to lose
face?...).
Also:
sorted_data = sorted(data, key=sort_key) given:
def sort_key(item):
return item.attr1, item.attr2
isn't light-weight compared to anonymous functions that other languages
have.
The whole point of anonymous functions in e.g. Javascript is that
embedding them in another statement or expression makes it a very
natural way of writing code. The "given" syntax doesn't achieve this
IMO; it forces you to write two additional keywords ("given" and "def")
and also write twice a function name that's totally useless - since you
can't reuse it anyway, as pointed out by Raymond.
I'm -1 on that syntax.
Regards
Antoine.
More information about the Python-ideas
mailing list