Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Mind your own business, Spock. I'm sick of your halfbreed interference.


devel / comp.lang.python / Re: Subtle difference between any(a list) and any(a generator) with Python 3.9

SubjectAuthor
* Subtle difference between any(a list) and any(a generator) withast
+- Re: Subtle difference between any(a list) and any(a generator) withChris Angelico
+- Re: Subtle difference between any(a list) and any(a generator) withTerry Reedy
`- Re: Subtle difference between any(a list) and any(a generator) withChris Angelico

1
Subtle difference between any(a list) and any(a generator) with Python 3.9

<61027753$0$3715$426a74cc@news.free.fr>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!cleanfeed1-b.proxad.net!nnrp1-1.free.fr!not-for-mail
Newsgroups: comp.lang.python
X-Mozilla-News-Host: news://news.free.fr:119
From: ast...@invalid (ast)
Subject: Subtle difference between any(a list) and any(a generator) with
Python 3.9
Date: Thu, 29 Jul 2021 11:39:30 +0200
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: fr
Content-Transfer-Encoding: 7bit
Lines: 40
Message-ID: <61027753$0$3715$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 29 Jul 2021 11:39:31 CEST
NNTP-Posting-Host: 91.170.32.5
X-Trace: 1627551571 news-1.free.fr 3715 91.170.32.5:9961
X-Complaints-To: abuse@proxad.net
 by: ast - Thu, 29 Jul 2021 09:39 UTC

Hello

Reading PEP572 about Python 3.9 assignment expressions,
I discovered a subtle difference between any(a list)
and any(a generator)

see:

>>> lines = ["azerty", "#qsdfgh", "wxcvbn"]
>>> any((comment := line).startswith('#') for line in lines)
True
>>> comment
"#qsdfgh"

>>> any([(comment := line).startswith('#') for line in lines])
True
>>> comment
'wxcvbn'

The two code snippets which seems very similar provide a
different value for "comment".

When "any" deals with a generator, it stops as soon it finds
a True value and returns True.

When "any" deals with a list, the whole list is calculated
first, and then "any" looks for a True.

Before 3.9 and the walrus operator, the two ways always provide
the same result, in a faster way with a generator.

With 3.9 and the walrus operator, result can be different

Re: Subtle difference between any(a list) and any(a generator) with Python 3.9

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Subtle difference between any(a list) and any(a generator) with
Python 3.9
Date: Thu, 29 Jul 2021 19:59:30 +1000
Lines: 26
Message-ID: <mailman.285.1627552783.4164.python-list@python.org>
References: <61027753$0$3715$426a74cc@news.free.fr>
<CAPTjJmrXUEqh8hS1SK8jM1KyhcWPi=xmuUmqPdxJ0H7bTdS7CA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de j/VTOKEicjlkz81fHn0MFgW1UR1hMbFYU1QnMDjO6+lA==
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=ked5oUW5;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.007
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'jul': 0.04; 'ast': 0.09;
'expression': 0.09; 'meant': 0.09; 'subject:between': 0.09;
'subject:list': 0.11; 'subject:Python': 0.12; '3.9': 0.16;
'chrisa': 0.16; 'evaluated': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; "isn't.": 0.16; 'lines)': 0.16;
'wrote:': 0.16; 'python': 0.16; 'thu,': 0.20; 'to:addr:python-
list': 0.23; 'lines': 0.24; '>>>': 0.26; 'comment': 0.28;
'difference': 0.32; 'to:name:python': 0.32; 'message-
id:@mail.gmail.com': 0.33; 'received:209.85.166': 0.33; 'header
:In-Reply-To:1': 0.33; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; "that's": 0.37; 'received:209.85':
0.38; 'received:209': 0.38; 'list': 0.39; 'subject:) ': 0.60;
'true': 0.63; 'subject:(': 0.65; 'exactly': 0.69; 'generator':
0.69; 'discovered': 0.81; '2021': 0.84; 'subtle': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=u0H7WP9g5tm4vPXj38E8I8I1SzlMvIS2xH5HUK0UAz0=;
b=ked5oUW5hKPYsZ5I99Qxnefv4oElUF6orSgefqbNOm1wvVdCEPIb/dS192VD6jzgKq
QXTpZuTQY8C1eyf3oR2FGCMEqv8+XY7gUaUzuQJk57TZHUMpx1w30RzxfhvlaDh0fnPY
+Dplstmfigsp8AWfaWHnhzNNhA7o3hMO9JNZ0Ws9wgb7fm1qH6GNIbQxCP9ZB64oK1yu
BtPCj+tAAZaILn0fgrQ7m1qzcuOviIJ6GvoJPcahdOp1uG8bC3vTMpq/kmZ38ciYMtSG
lutufKwNJOSfI1qIi3qAda2LH/kU+X/uu8rTP/9ZSgP/tT79oJwQF6n680ZlQCWk+nj1
WhyA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=u0H7WP9g5tm4vPXj38E8I8I1SzlMvIS2xH5HUK0UAz0=;
b=ObmhpP1tl7PnzjF2be9/AqMvYZ0N5KPVTHPDAg6jAr6JOd33OHGfUIYpIaWCi9ARS3
Ta7Ylgd4AMxePVxHLEP0SnSX8NcgFcpSg9mzF1/PrXeO10WCwFIjj+rLfKbjPZLr1AYD
eAYJpyA1yTEq1kfzkVwUVmbBnP9d3RMjr8wlisGd3Lrz7thGyHHAuCKhWHByQLG4cOL5
v7+TCUa/m0+Nz4Tgfw8D5zwbppRNoAqveM3v1hktozVy6ZpJrtRfhQ7MckSRZ7MPAevD
wiMeD83RBZhnBO5oUvuGqOMXg7ANVFXDVRL4emYizOYSh7DAkR/FG8iJHPI8/JLOkJU4
t8EA==
X-Gm-Message-State: AOAM532YJoCaQmqpwgN2UyBeDBRoYPk+36lVomsEIEkY55WATktSdNQZ
s50LjxOurErR/6wEk2V66mah8ayvARjYLnHNlAoEej1U/oo=
X-Google-Smtp-Source: ABdhPJycY9TOyC5wKSkxH08qYFhCbyzTp0tQZ2pUDAkxOYeLbivBsPSCE1eOdwvA4dml7nk0/cceFOqVqwRJSrC7JIA=
X-Received: by 2002:a05:6638:2195:: with SMTP id
s21mr3797864jaj.15.1627552781155;
Thu, 29 Jul 2021 02:59:41 -0700 (PDT)
In-Reply-To: <61027753$0$3715$426a74cc@news.free.fr>
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: <CAPTjJmrXUEqh8hS1SK8jM1KyhcWPi=xmuUmqPdxJ0H7bTdS7CA@mail.gmail.com>
X-Mailman-Original-References: <61027753$0$3715$426a74cc@news.free.fr>
 by: Chris Angelico - Thu, 29 Jul 2021 09:59 UTC

On Thu, Jul 29, 2021 at 7:49 PM ast <ast@invalid> wrote:
>
> Hello
>
> Reading PEP572 about Python 3.9 assignment expressions,
> I discovered a subtle difference between any(a list)
> and any(a generator)
>
> see:
>
> >>> lines = ["azerty", "#qsdfgh", "wxcvbn"]
> >>> any((comment := line).startswith('#') for line in lines)
> True
> >>> comment
> "#qsdfgh"
>
> >>> any([(comment := line).startswith('#') for line in lines])
> True
> >>> comment
> 'wxcvbn'

"any" is irrelevant here. What you're seeing is that a list
comprehension is evaluated fully, and a generator expression isn't.
That's exactly what they're meant to do :)

ChrisA

Re: Subtle difference between any(a list) and any(a generator) with Python 3.9

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: tjre...@udel.edu (Terry Reedy)
Newsgroups: comp.lang.python
Subject: Re: Subtle difference between any(a list) and any(a generator) with
Python 3.9
Date: Thu, 29 Jul 2021 14:14:11 -0400
Lines: 49
Message-ID: <mailman.292.1627587523.4164.python-list@python.org>
References: <61027753$0$3715$426a74cc@news.free.fr>
<sdur5l$ic2$1@ciao.gmane.io>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de h6/+c02XEyioSnJeSFfD7gumMA63nm6mUqPrwgtn0jgA==
Return-Path: <python-python-list@m.gmane-mx.org>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'message-
id:@ciao.gmane.io': 0.09; 'received:ciao.gmane.io': 0.09;
'received:gmane.io': 0.09; 'received:list': 0.09; 'snippets':
0.09; 'subject:between': 0.09; 'terry': 0.09; 'looks': 0.11;
'subject:list': 0.11; 'subject:Python': 0.12; '3.9': 0.16;
'from:addr:udel.edu': 0.16; 'iteration': 0.16; 'lines)': 0.16;
'received:116.202': 0.16; 'received:116.202.254': 0.16;
'received:116.202.254.214': 0.16; 'wrote:': 0.16; 'python': 0.16;
'returns': 0.23; 'to:addr:python-list': 0.23; 'list,': 0.23;
'lines': 0.24; 'code': 0.24; 'idea': 0.25; '>>>': 0.26; 'seems':
0.26; 'first,': 0.27; 'comment': 0.28; 'header:User-Agent:1':
0.31; 'am,': 0.31; 'difference': 0.32; 'unknown': 0.32; 'header
:In-Reply-To:1': 0.33; 'same': 0.34; 'two': 0.37; 'way': 0.37;
'mean': 0.37; 'though': 0.38; 'list': 0.39; 'subject:) ': 0.60;
'true': 0.63; 'involve': 0.65; 'subject:(': 0.65; 'similar': 0.66;
'result,': 0.69; 'received:116': 0.71; '1st': 0.71; 'finds': 0.77;
'discovered': 0.81; 'subtle': 0.84
X-Injected-Via-Gmane: http://gmane.org/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
In-Reply-To: <61027753$0$3715$426a74cc@news.free.fr>
Content-Language: en-US
X-Mailman-Approved-At: Thu, 29 Jul 2021 15:38:43 -0400
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: <sdur5l$ic2$1@ciao.gmane.io>
X-Mailman-Original-References: <61027753$0$3715$426a74cc@news.free.fr>
 by: Terry Reedy - Thu, 29 Jul 2021 18:14 UTC

On 7/29/2021 5:39 AM, Unknown wrote:
> Hello
>
> Reading PEP572 about Python 3.9 assignment expressions,
> I discovered a subtle difference between any(a list)
> and any(a generator)
>
> see:
>
> >>> lines = ["azerty", "#qsdfgh", "wxcvbn"]
> >>> any((comment := line).startswith('#') for line in lines)
> True
> >>> comment
> "#qsdfgh"
>
> >>> any([(comment := line).startswith('#') for line in lines])

Same as

>>> booleans = [(comment := line).startswith('#') for line in lines]
>>> # comment == last item.
>>> any(booleans) # Iteration though booleans stops at 1st True.

> True
>>> comment
> 'wxcvbn'
>
> The two code snippets which seems very similar provide a
> different value for "comment".
>
> When "any" deals with a generator, it stops as soon it finds
> a True value and returns True.
>
> When "any" deals with a list, the whole list is calculated
> first, and then "any" looks for a True.
>
> Before 3.9 and the walrus operator, the two ways always provide
> the same result, in a faster way with a generator.

Since the 'two ways' involve the new :=, I have no idea what 'two ways'
and 'same result' you mean before :=.

> With 3.9 and the walrus operator, result can be different

--
Terry Jan Reedy

Re: Subtle difference between any(a list) and any(a generator) with Python 3.9

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Subtle difference between any(a list) and any(a generator) with
Python 3.9
Date: Fri, 30 Jul 2021 05:59:00 +1000
Lines: 28
Message-ID: <mailman.293.1627588753.4164.python-list@python.org>
References: <61027753$0$3715$426a74cc@news.free.fr>
<sdur5l$ic2$1@ciao.gmane.io>
<CAPTjJmpLJWe-g9Vps4aavdk2tWqKJkQcAoWotpNj5Gza-Ya33A@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de vdSocRvHi1zlKNHAh6Ys2A4c4Zqzm3cjsmO1xPypbdrg==
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=FipEp3k1;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'this:': 0.03; 'comments':
0.04; 'jul': 0.04; 'filter': 0.07; 'matches': 0.07; 'else:': 0.09;
'happens.': 0.09; 'subject:between': 0.09; 'terry': 0.09;
'subject:list': 0.11; 'subject:Python': 0.12; 'chrisa': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'none)': 0.16; 'personally,': 0.16; 'wrote:': 0.16; 'fri,': 0.23;
"i'd": 0.23; 'to:addr:python-list': 0.23; 'lines': 0.24; 'idea':
0.25; 'depends': 0.26; 'notes': 0.27; 'comment': 0.28; "isn't":
0.29; 'everyone': 0.29; 'there': 0.31; 'think': 0.31; 'but': 0.31;
"i'm": 0.32; '"this': 0.32; 'do.': 0.32; 'to:name:python': 0.32;
'message-id:@mail.gmail.com': 0.33; 'received:209.85.166': 0.33;
'header:In-Reply-To:1': 0.33; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'neither': 0.37; 'people': 0.37;
'mean': 0.37; 'received:209.85': 0.38; 'read': 0.38;
'received:209': 0.38; 'use': 0.38; 'pretty': 0.40; 'normal': 0.60;
'subject:) ': 0.60; 'search': 0.61; 'lot': 0.62; 'everything':
0.64; 'your': 0.64; 'involve': 0.65; 'subject:(': 0.65; 'credit':
0.68; 'features': 0.76; '2021': 0.84; 'capturing': 0.84;
'cleaner': 0.84; 'newest': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=WQxqXINx604Q1MZ37uTi8MZfSi+fJd7iaO2dK9Ibcmw=;
b=FipEp3k1FNoIOAc7jThRJgNanPDPscdKA5wNxLXdeuQPpNVZ/4JcyWHMdatHEWa14y
p549Yt+90YMgXD/+6PQImS1mCL88LUn843+KNpv7jGzcjPdkT4hcvfRhnku+wJI3ffEL
496idqR3F2lE4D3jwxDmHb4e8AB+FcoQub1OpD0WDgO8vkeFJSPXyMP3LDLGonM4hV9x
ydlqyWLkgAzKIM80YqerkcWf3m5UVKo38Mj/VKjV/SxJcTU6cIdrma/PCZznBsX2mLxY
IKPsZDY8vYgVAZAM5B+uQn/UuPEvOUBw5jmNekyW/o6F8jTxNJg/h7URQKcE3ChlOFHo
IFqQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=WQxqXINx604Q1MZ37uTi8MZfSi+fJd7iaO2dK9Ibcmw=;
b=g1jB0/MANgaNIIC1ypQPyH1415+SxGFjHyzLM80KV2I8d+vjkKruHdfD9tsRInBG02
/mwaGEmna1e33cssPSMhwQwJM366x2Fcx5DxTpk/PpYsz0bBWEIP5m6fy9pZqFqZ3xJM
n6/qKPgrh8NCAwMYWBKIXdbQJdAOCgnmCraRknnnnKbSGIoO36t1oBBtXn/OZBq3yDfa
apNe2PbVe0p5f7lpXgymsbuc579gNmX3dSOmxtLSOxAY89ktNWwRZxei3w1JHjD4bXfw
+yXW7XoARae+HtziF44+pWkFVE/prQI/F0LjUMZLzGB8HJ2K+XxKRHnIlZ96v0o2m/94
Ko5g==
X-Gm-Message-State: AOAM531cvnoAeuGrHlvdmBYSghdVAejtlT5YsIDK0HlQ8+9zB3+l0OPO
W8YNwjG6m+tf1TZHKnGM3e3QkD2ZdKpoYHkCqiBBdilx
X-Google-Smtp-Source: ABdhPJzN4TGEulYtN9adv74ftB5Sw5Q0HrUJIDmLwiQ5R5wpF5wVx6BH70Mi4GNlsvQE8LQS0AaU45OQOEj3lt32MUY=
X-Received: by 2002:a5e:9918:: with SMTP id t24mr5491319ioj.24.1627588751522;
Thu, 29 Jul 2021 12:59:11 -0700 (PDT)
In-Reply-To: <sdur5l$ic2$1@ciao.gmane.io>
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: <CAPTjJmpLJWe-g9Vps4aavdk2tWqKJkQcAoWotpNj5Gza-Ya33A@mail.gmail.com>
X-Mailman-Original-References: <61027753$0$3715$426a74cc@news.free.fr>
<sdur5l$ic2$1@ciao.gmane.io>
 by: Chris Angelico - Thu, 29 Jul 2021 19:59 UTC

On Fri, Jul 30, 2021 at 5:40 AM Terry Reedy <tjreedy@udel.edu> wrote:
> Since the 'two ways' involve the new :=, I have no idea what 'two ways'
> and 'same result' you mean before :=.
>

I'm not sure, but I think that a lot of people read patch notes as if
they say "this is how everyone needs to do things now", and then blame
or credit the newest features with everything that happens.

Personally, I'd search for the comments like this:

for line in lines:
if line.startswith("#"):
... # whatever you do if there is one
break
else:
... # whatever you do if there isn't one

But if you have to do it with a one-liner, much cleaner to use next on
your filter:

comment = next((l for l in lines if l.startswith("#")). None)

Neither of these depends on :=. I don't know what "two ways" there
are, but capturing the first element that matches a filter is a pretty
normal thing to do.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor