Congrats to Chris for breaking his PEP curse

Steven D'Aprano steve+comp.lang.python at
Wed Jul 4 03:05:44 EDT 2018

On Tue, 03 Jul 2018 23:05:01 -0600, Ian Kelly wrote:

> Now that I've actually read the PEP (sorry, I just assumed it would
> never fly), I have a couple of (tongue-in-cheek) observations about it:
>> group = re.match(data).group(1) if re.match(data) else None
> The only problem with this example of doing more work to save a line of
> code is that presumably the same programmer would now instead write:
>> group = if match := re.match(data) else None

There's no stopping some people from making poor naming choices. Surely 
the standard name for match objects is "mo", not match?

But if people were going to write that, surely they would now write:

    match = re.match(pattern, data)
    group = if match else None

and I don't see how spreading it over two statements makes it better. 
Either you can tell the difference between re.match(pattern, data) and 
match, in which case one line or two makes little difference, or you 
can't, in which case one line or two makes little difference.

group = re.match(pat, data).group(1) if re.match(pat, data) else None

group = if mo := re.match(pat, data) else None

The second works for me.

> Secondly, I believe the order of evaluation means that, much as I would
> like to, I can't now write all my statements as:
> f(f := lambda f: do_something())
> Because the outer f will be evaluated before the argument expression
> assigns it.

Watch out that you don't poke your tongue all the way through your 
cheek :-)

> Well, that's no fun. Is it too late to fix this? I'm only
> half-joking here;

It doesn't work now:

class X: pass

obj = X()
obj.f(setattr(obj, "f", lambda f: print(f+1)))

raises AttributeError, as it bloody well ought to :-)

> if it worked, this would be a much better way to pass
> a lambda to itself for recursion than the existing idiom:
> (lambda f: lambda *a: f(f, *a))(lambda s, x: 1 if x==0 else x * s(s,
> x-1))(10)

/me reads that code

"Kill it! Kill it with fire!!!"

That was my *second*, more polite, reaction :-)

> That's a factorial function, in case you couldn't tell.

Well obviously.

> Oh well, at least now I'll be able to write exec(x := 'del x').

I am *so* going to use that instead of "pass" from now on ;-)

Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

More information about the Python-list mailing list