Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"Indecision is the basis of flexibility" -- button at a Science Fiction convention.


devel / comp.lang.python / Re: Why operations between dict views return a set and not a frozenset?

SubjectAuthor
o Re: Why operations between dict views return a set and not aMarco Sulla

1
Re: Why operations between dict views return a set and not a frozenset?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: Marco.Sulla.Python@gmail.com (Marco Sulla)
Newsgroups: comp.lang.python
Subject: Re: Why operations between dict views return a set and not a
frozenset?
Date: Tue, 11 Jan 2022 00:25:57 +0100
Lines: 100
Message-ID: <mailman.146.1641857197.3079.python-list@python.org>
References: <CABbU2U9EBev-MWb+5j=uUZJaYAtKHw+raAviOFpd50d9ojsA2w@mail.gmail.com>
<CAPTjJmoRm26RiPsfhSph_Zwj=gJFyDN47u3kcvxfQPSM+fTf1g@mail.gmail.com>
<CABbU2U9Pazu_yjdm8veY+VnsGLpq9axSEXmF-eJrTV3ZjHax3g@mail.gmail.com>
<CAPTjJmoW0gV0dp9W=m=taB5vFZ4yy+DwAj9H9OUH+qXDkzhmmw@mail.gmail.com>
<CABbU2U_EPGTgGkhiMLxg86o9-NyxLXc9jGLBE-qf8jnG4fM-Sw@mail.gmail.com>
<CAPTjJmq=_Fo9ArCu-r0wZB-70WL0AjOqWttGqb_TLT62HBPnfQ@mail.gmail.com>
<CABbU2U_cV2m=QoNG+6w2RvRR05pLuoEyYFk8GbLx6P2zqPQWgg@mail.gmail.com>
<CAPTjJmqJ+nZfO-T6bkCbSERVko2TbQP5g11y_oB0+ZgUx06cfw@mail.gmail.com>
<CABbU2U_d+Sj8naMwz8jWPbc2+2=z1EmoSRYwjnAeA9Dceg1F=g@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de jCKHCwC/igUk9p80t9CTQgK84fZneqTicVSUNaWUjNOw==
Return-Path: <elbarbun@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=TTKi/0AM;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'traceback': 0.04; '(most':
0.05; '2022': 0.05; 'is.': 0.05; 'last):': 0.05; 'explicitly':
0.07; 'guido': 0.07; 'subject:Why': 0.07; 'angelico': 0.09;
'anyway,': 0.09; 'cc:addr:python-list': 0.09; 'fewer': 0.09;
'linux': 0.09; 'methods,': 0.09; 'objects,': 0.09; 'ok,': 0.09;
'received:209.85.219': 0.09; 'set,': 0.09; 'speed,': 0.09;
'subject:between': 0.09; 'subject:not': 0.09; 'typeerror:': 0.09;
'yes.': 0.09; '(more': 0.16; 'arguments': 0.16; 'cases,': 0.16;
'cc:name:python list': 0.16; 'dict': 0.16; 'explored': 0.16;
'from:name:marco sulla': 0.16; 'hand,': 0.16; 'hypothesis': 0.16;
'indeed': 0.16; 'items:': 0.16; 'iteration': 0.16; 'mathematical':
0.16; 'me?': 0.16; 'slow': 0.16; 'subject:operations': 0.16;
'tuple': 0.16; 'understand.': 0.16; 'values,': 0.16; 'values?':
0.16; 'view.': 0.16; 'wrote:': 0.16; 'python': 0.16; 'api': 0.17;
'says': 0.17; 'values': 0.17; "can't": 0.17; "aren't": 0.19;
'thu,': 0.19; 'cc:addr:python.org': 0.20; 'all,': 0.20; 'maybe':
0.22; 'version': 0.23; 'code': 0.23; 'views': 0.23; 'list,': 0.24;
'cc:2**0': 0.25; 'object': 0.26; "isn't": 0.27; 'function': 0.27;
'>>>': 0.28; 'chris': 0.28; 'fact': 0.28; 'sense': 0.28; 'this?':
0.29; 'deep': 0.31; 'default': 0.31; 'think': 0.32; '"",': 0.32;
'course.': 0.32; 'here,': 0.32; 'objects': 0.32; 'returning':
0.32; 'said,': 0.32; 'transform': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33; 'there':
0.33; "didn't": 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'trying': 0.35; 'automatic': 0.35;
'item': 0.35; 'majority': 0.35; 'meaning': 0.35;
'from:addr:gmail.com': 0.35; 'cases': 0.36; 'change': 0.36;
'currently': 0.37; "it's": 0.37; 'received:209.85': 0.37; 'file':
0.38; 'received:209': 0.39; 'added': 0.39; 'least': 0.39;
'mentioned': 0.39; 'use': 0.39; 'break': 0.39; 'methods': 0.39;
'neither': 0.39; 'rest': 0.39; 'wed,': 0.39; 'explain': 0.40;
'true.': 0.40; 'both': 0.40; 'something': 0.40; 'want': 0.40;
'should': 0.40; 'skip:o 10': 0.61; 'ever': 0.63; 'simply': 0.63;
'everything': 0.63; 'pass': 0.64; 'down': 0.64; 'full': 0.64;
'operations.': 0.64; 'well': 0.65; 'less': 0.65; 'accept': 0.67;
'that,': 0.67; 'items': 0.68; 'matter': 0.68; 'operations': 0.68;
'before,': 0.69; 'perfectly': 0.69; 'type:': 0.69; 'performance':
0.71; 'quote': 0.74; 'operate': 0.75; 'more.': 0.82; 'perfect':
0.82; 'ad-hoc': 0.84; 'bothered': 0.84; 'difference.': 0.84;
'massive': 0.84; 'of...': 0.84; 'sulla': 0.84; 'doing.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=Hvd1H6pR6DlI18/jkq7OHoXFeOCmcTEZcmym0Rd7BNA=;
b=TTKi/0AMgJWqJVWkkUa9ePWe8G39wicNVn33cSRPk+BkxZKmMMmwmcKQPr5GZxFiqZ
JPGK3kh2E4Z7evGf722G7H5/MT+xIlRXcP2qKIHiGIFvL/6bUZ9RlnX/MSTPAqQHqB/3
I0ABzAnp9g2Pxl6Leh8MbFar4tkh5NMwcmuux6cESi8U+0a0vpugjyY6OA8oj7vWW1IM
MP2GLK2sAsbKYEtSyUNOL/rQBPWv7ltEnWX83X1tEXencYsXGPE+JQtP0EGMS5IovUYl
qXPsaG1r8gnE+2Vc0MThn8NTfJwDeYcfph/ag+FQAZu8vy7fiEOGVgG5yFGKgJIwGclp
g3Mw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc;
bh=Hvd1H6pR6DlI18/jkq7OHoXFeOCmcTEZcmym0Rd7BNA=;
b=ubqVtuaAw3NaU3q6STtmRb9GFVt5NXvwYk4gvNWOxn0m0rn2g7Hu9S3a+vByjWo5ys
e5YqXY/8wM88dVCv7RmrXZ9nnVpEC98Cob2HPZFaYrho6W5kwJBpXoSWbbrd2/mnVd1u
cDTT7DwvOFFO2rOTx8tkc3oT9yVERR2QFeIXKrYRHsMW0fbIMubanc+9ag0hoUVfuQoi
Zdl6kD1jCE+1wJmR0KfoVY7Ep9Pj11m8BHC6DFOP6bvNiklwc1ZjPj07qk3w/PzkNLsy
+180Mg9PnWVeghDDQNk0HGrmF5IOSdosmOlmgwIqNdDkvVFg4NL+JG6UW0xHVgXkCXzz
sMow==
X-Gm-Message-State: AOAM533P2atDmhOWXtgsrXLVqPltpxsGohCFuUh3nJWNPfP1ScWCbmoX
YeDkHA0lWaR3i1NPDyV4DLVH68Sdf9MXksavgLktmxP5gHo=
X-Google-Smtp-Source: ABdhPJwjzCWYH3baUCco50a5TjKBEVpLu7oVHyIIr4+XmC8uDxzI1abuPkeYBwi8NFTJnzQ7ih864gPTRDqOm1Ctlzw=
X-Received: by 2002:a25:8b85:: with SMTP id j5mr2825567ybl.698.1641857194242;
Mon, 10 Jan 2022 15:26:34 -0800 (PST)
In-Reply-To: <CAPTjJmqJ+nZfO-T6bkCbSERVko2TbQP5g11y_oB0+ZgUx06cfw@mail.gmail.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <CABbU2U_d+Sj8naMwz8jWPbc2+2=z1EmoSRYwjnAeA9Dceg1F=g@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U9EBev-MWb+5j=uUZJaYAtKHw+raAviOFpd50d9ojsA2w@mail.gmail.com>
<CAPTjJmoRm26RiPsfhSph_Zwj=gJFyDN47u3kcvxfQPSM+fTf1g@mail.gmail.com>
<CABbU2U9Pazu_yjdm8veY+VnsGLpq9axSEXmF-eJrTV3ZjHax3g@mail.gmail.com>
<CAPTjJmoW0gV0dp9W=m=taB5vFZ4yy+DwAj9H9OUH+qXDkzhmmw@mail.gmail.com>
<CABbU2U_EPGTgGkhiMLxg86o9-NyxLXc9jGLBE-qf8jnG4fM-Sw@mail.gmail.com>
<CAPTjJmq=_Fo9ArCu-r0wZB-70WL0AjOqWttGqb_TLT62HBPnfQ@mail.gmail.com>
<CABbU2U_cV2m=QoNG+6w2RvRR05pLuoEyYFk8GbLx6P2zqPQWgg@mail.gmail.com>
<CAPTjJmqJ+nZfO-T6bkCbSERVko2TbQP5g11y_oB0+ZgUx06cfw@mail.gmail.com>
 by: Marco Sulla - Mon, 10 Jan 2022 23:25 UTC

On Wed, 5 Jan 2022 at 23:02, Chris Angelico <rosuav@gmail.com> wrote:
>
> On Thu, Jan 6, 2022 at 8:01 AM Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
> >
> > On Wed, 5 Jan 2022 at 14:16, Chris Angelico <rosuav@gmail.com> wrote:
> > > That's an entirely invisible optimization, but it's more than just
> > > "frozenset is faster than set". It's that a frozenset or tuple can be
> > > stored as a function's constants, which is a massive difference.
> >
> > Can you explain this?
>
> Play around with dis.dis and timeit.

? I don't understand. You're talking about function constants. What
are they? I can't dig deep into something if I can't know what it is.
Maybe are you talking about function default values for parameters?

> > > Function positional arguments aren't interchangeable, so it makes
> > > sense to have them as a tuple.
> >
> > You are wrong, since kwarg is a dict. Indeed I proposed to use
> > frozendict for kwargs, and Guido said that it's a pity that this will
> > break a lot of existing Python code :D, since the fact that args is
> > _immutable_ and kwargs not always bothered him.
>
> Excuse me? I mentioned kwargs in the part that you removed from the
> quote, and the part you're quoting explicitly says "positional
> arguments".

Ok, I quote also the other part:

> (Function *keyword* arguments, on the other hand, are different; as
> long as the mapping from keys to values is maintained, you can remove
> some of them and pass the rest on, without fundamentally changing
> their meaning.)

First of all, I repeat, Guido said (more or less) that in a perfect
world, kwargs are immutable. Or maybe I did not understand what he
said, maybe he said that in a perfect world also args are mutable. But
I suppose it's more probable the first hypothesis :D

Secondly, you can also get the args from a function, transform it in a
list, change something and pass it unpacked to another function. You
will not change the meaning of the tuple, since, well, you copied it
in another mutable object. The original object is untouched.

I perfectly agree that, in the majority of cases, returning an
immutable vs a mutable are a matter of... sense? Meaning? Ok, I
perfectly agree. But IMHO there are many cases in which immutable
objects are used for a matter of speed, and I bet that args is one of
them.

> > Anyway, I'm starting to think that neither set nor frozenset are good
> > for dict items:
> >
> > (venv_3_10) marco@buzz:~$ python
> > Python 3.10.0 (heads/3.10-dirty:f6e8b80d20, Nov 18 2021, 19:16:18)
> > [GCC 10.1.1 20200718] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> a = {1: 2}
> > >>> b = {3: []}
> > >>> a | b
> > {1: 2, 3: []}
> > >>> a.items() | b.items()
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in <module>
> > TypeError: unhashable type: 'list'
> > >>>
>
> Well yes. Only dict keys can be considered to be set-like.

This is not true. It's at least from Python 3.6, and I think also
before, that almost the full Set API was added to both keys and items
view.
Items indeed are a sort of set, in a mathematical sense, since any
pair (key, value) is unique, even if value is mutable.

> I don't
> know WHAT you think you're trying to do here, but if you ever thought
> of set operations on dict values, you may want to completely rethink
> what you're doing.

set ops on values? Never said that :) I said that currently you can
operate on item views with set operators. This is a fact.

I also said that, since py sets accept only hashable objects, maybe
another ad-hoc object should be used for the result of the items
operations.
But maybe the change isn't worth the additional trouble. Indeed I
didn't know about the new set methods and operations on dict views
until I explored dictobject.c

> Performance is not an automatic result of immutability. That simply
> isn't how it works.

Of course. You can use a proxy and slow down almost everything much
more. Or you can simply create a version of the mutable object with
fewer methods, as more or less frozenset is. I checked the
implementation, no fast iteration is implemented. I do not understand
why in `for x in {1, 2, 3}` the set is substituted by a frozenset.


devel / comp.lang.python / Re: Why operations between dict views return a set and not a frozenset?

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor