On Thu, Apr 12, 2018 at 12:28:23AM +1000, Chris Angelico wrote:
> On Thu, Apr 12, 2018 at 12:11 AM, Paul Moore <p.f.mo...@gmail.com> wrote:
> > On 11 April 2018 at 14:54, Chris Angelico <ros...@gmail.com> wrote:
> >> Sure, if you're just assigning zero to everything. But you could do
> >> that with a statement. What about this:
> >>
> >> q = {
> >>     lambda: x := lambda y: z := a := 0,
> >> }
> >>
> >> Yes, it's an extreme example, but look at all those colons and tell me
> >> if you can figure out what each one is doing.
> >
> > lambda: x := (lambda y: (z := (a := 0)))
> >
> > As I say, it's the only *possible* parsing. It's ugly, and it
> > absolutely should be parenthesised, but there's no need to make the
> > parentheses mandatory. (And actually, it didn't take me long to add
> > those parentheses, it's not *hard* to parse correctly - for a human).

I agree with Paul, except I think he's added too many parens. Chained
assignments ought to be obvious enough that we can dispense with the 
extras:

    lambda: x := (lambda y: (z := a := 0))

I know that they are legal, but I really dislike *pointless* examples 
that bind to a name and then never use it. If we're to get a good feel 
for how complex these expressions are going to be, they ought to be 
realistic -- even if that makes them more complex.

And I'm not terribly disturbed by excessively obfuscated examples. The 
answer to obfuscated code is, Don't Do That.

So we should consider complex examples which are *realistic*, not ones 
designed intentionally as obfuscated code. So, with that advice, let's 
take your q example from above, and re-work it into something which is 
at least potentially realistic, of a sort.

We want q to be a set consisting of a factory function which takes a 
single argument (different from your example, I know), builds an inner 
function, then returns that function and the result of that function 
called with the original argument:

def factory(arg):
    def inner(y):
        a := z := y + 1  # seems kinda pointless to me, but okay...
        return (a, a+z, a*z)
    return (inner, inner(arg))

q = {1, 2, factory, 3, 4}

Now let's re-write it in using expression assignment:

q = {1, 
     2,
     (lambda arg: 
          lambda y: (a := (z := y + 1), a+z, z*z)
     ),
     3,
     4,
    }

Not too awful, although it is kinda pointless and not really a great 
justification for the feature. Let's obfuscate it:

q = {1, 2, (lambda arg: lambda y: a := z := y + 1, a+z, z*z), 3, 4}

I've seen worse :-)


-- 
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to