Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Nonsense. Space is blue and birds fly through it. -- Heisenberg


devel / comp.lang.python / Confusing error message: lambda walruses

SubjectAuthor
o Confusing error message: lambda walrusesChris Angelico

1
Confusing error message: lambda walruses

<mailman.911.1633217991.4164.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=19944&group=comp.lang.python#19944

  copy link   Newsgroups: comp.lang.python
Path: rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Confusing error message: lambda walruses
Date: Sun, 3 Oct 2021 10:39:38 +1100
Lines: 61
Message-ID: <mailman.911.1633217991.4164.python-list@python.org>
References: <CAPTjJmoTZhKg=cD1myfEPZFAHbc-tvffy0KL+emp3+tuht0LhA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de ioRKXIlJ9U0NSqvolp4NjQ8N5JM8X/F2SXjSRFqEOoUA==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=I3SBStTR;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; 'parameter':
0.05; 'subject:error': 0.07; 'linux': 0.09; 'subject:Confusing':
0.09; 'syntax': 0.15; '(1,': 0.16; '*think*': 0.16; 'chrisa':
0.16; 'confusing.': 0.16; 'expressions': 0.16; 'from:addr:rosuav':
0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'oh,':
0.16; 'outer': 0.16; 'received:209.85.221.47': 0.16; 'received
:mail-wr1-f47.google.com': 0.16; 'syntaxerror:': 0.16; 'python':
0.16; 'to:addr:python-list': 0.20; "what's": 0.22; 'cannot': 0.25;
'function': 0.27; '>>>': 0.28; 'example,': 0.28; "doesn't": 0.32;
'"",': 0.32; 'to:name:python': 0.32; 'message-id:@mail.gmail.com':
0.32; 'but': 0.32; 'able': 0.34; 'received:google.com': 0.34;
'fine': 0.35; 'invalid': 0.35; 'from:addr:gmail.com': 0.35;
'functions': 0.36; 'using': 0.37; "it's": 0.37; 'received:209.85':
0.37; 'file': 0.38; 'received:209': 0.39; 'two': 0.39; 'use':
0.39; 'subject:message': 0.40; 'try': 0.40; 'shows': 0.67; '....':
0.76
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:from:date:message-id:subject:to;
bh=+TYiqWGSFwWttv1SaWtVdL4IbbuYMbDH0ENsbiOzfnM=;
b=I3SBStTRhYUvV73Sqiq012trnMVf4Xmst5VydOIJbubPX6p3vhKzSy4Ajno9t694Gm
SieawCaRKk8tFBtoIXPyFozz62K7svkRKfAK0u3KY4aCaunQYyhbyiYTrnPb2mgMysYh
Fu4w8XMULhRhhcvCtqk1RvX+70GSPcBKyDsGT43aWSmg+ydAfhP7mDoRR34D1VN+P+M6
UMZtl33B0iKzyj1Z7HOSN0/Nue+FVmnnf96fDwYr/tuxfOI4uDRFeIqni7kIQRn4nB/x
bpy2yC0aOgW65RpZUsErk8wFPKCleOc8C+TA0cujhcJNBE0ZqwERF1+H7xhwn/NtI+VZ
7KyQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:from:date:message-id:subject:to;
bh=+TYiqWGSFwWttv1SaWtVdL4IbbuYMbDH0ENsbiOzfnM=;
b=snJqV+omJD7tO93ZvqujIl8/ETUSHL+jFsuz2r3C83LBpCfGAL8ePRn6uWO25akk0Q
NeYYynYXOaoqQN7PoKXPW3/JXvJlz09uQhPhIz56Gn2mrgLS/35ks0/j74o/B9eevUec
8zeaDr16zjmKLwiW/sptmFRwajhO/0c12t/s1rPnf43OU3qCwj4gCw7+LqSEya55TvCa
vNVUk2o+bMmiKDN7tDxpA27omf+vG9UAYAHLywrSDBMwC/8c8bewkG60Zs8OLWKDbMhq
GYNJ5uUOE9jnICx0rat9wr0YMcw3I8QIBmSAS1Dlues0jMF3d9QHytjufz8NIG/paS2U
m8jg==
X-Gm-Message-State: AOAM531qNsEPxZJtg78x+dR9vewELP8uIWJFbBcOyS7e04Sq/0/XbuMW
ymDR6+aNMY0Dg5XHG0sI5faojg0ZuPjsKMNygLzHKPGA0T6fQQ==
X-Google-Smtp-Source: ABdhPJxSfmM4M5BFKVu7OA4Ck0TBW6QhHtaV/VlzJPZntwX3HF9WyhMZQaHOOSxgCEVdoUEkVaFKMXg0ChgGSvty7tg=
X-Received: by 2002:a5d:6288:: with SMTP id k8mr5635101wru.137.1633217989166;
Sat, 02 Oct 2021 16:39:49 -0700 (PDT)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmoTZhKg=cD1myfEPZFAHbc-tvffy0KL+emp3+tuht0LhA@mail.gmail.com>
 by: Chris Angelico - Sat, 2 Oct 2021 23:39 UTC

Using assignment expressions in lambda functions sometimes works, but
sometimes doesn't.

Python 3.11.0a0 (heads/main:dc878240dc, Oct 3 2021, 10:28:40) [GCC
8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

# Fine if it's a parameter to the lambda function
>>> def f():
.... return lambda x: (x, x := 2, x)
....
>>> g = f()
>>> g(1)
(1, 2, 2)

# Plain ol' SyntaxError if unparenthesized
>>> def f(n):
.... return lambda: n := 1
File "<stdin>", line 2
return lambda: n := 1
^^
SyntaxError: invalid syntax

# Fine if it's a parameter to the outer function
>>> def f(n):
.... return lambda: (n := 1)
....
>>> g = f(3)
>>> g()
1

# But a SyntaxError if parenthesized like this??
>>> def f(n):
.... return (lambda: n := 1)
File "<stdin>", line 2
return (lambda: n := 1)
^^^^^^^^^
SyntaxError: cannot use assignment expressions with lambda

# Oh, and it doesn't actually assign anything.
>>> def f(n):
.... return (lambda: (n := 1)), (lambda: n)
....
>>> g, h = f(5)
>>> h()
5 >>> g()
1 >>> h()
5

What's going on here?

Disassembly of the last function shows that it's using STORE_FAST to
try to assign to n, so I *think* that it's actually like the first
example, where the lambda function is able to assign to its own
locals, whether they're parameters or not. But the two SyntaxErrors
are confusing.

ChrisA


devel / comp.lang.python / Confusing error message: lambda walruses

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor