Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLiSz-0002is-Ig for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 07:48:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLiSy-0005Ey-Ci for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 07:48:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLiSy-0005Er-5x for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 07:48:28 +0000 Received: from mout.gmx.net ([212.227.17.20]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLiSw-0005KT-Gi for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 07:48:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1601020104; bh=H8d0v8TIyb6crJS02zvB8+TKnXrovENB0iam4Q8hgVw=; h=X-UI-Sender-Class:Date:From:To:Subject:References:In-Reply-To; b=klnP8engzlwITiIRyclYJDdJBrrbDI21mVkQq2L+6NdaHLd1sJe6pu37dyT/jHJpD 28FtkaJ0GNwDYczHoWa/ZBM8rG6HGwOKjyZx8rAG+MBwzYihAvskZux3EggheL1/jA h/PRttJBKdo57Y+D7Id1iWQgC5uC5yeWH/YW5Wxk= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from hermes ([78.55.250.218]) by mail.gmx.com (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MYeR1-1k02zj0S03-00VkEH for ; Fri, 25 Sep 2020 09:48:24 +0200 Received: from ncq by hermes with local (Exim 4.94) (envelope-from ) id 1kLiSq-0005Gq-K1 for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 09:48:20 +0200 Date: Fri, 25 Sep 2020 09:48:20 +0200 From: Karsten Hilbert To: psycopg@lists.postgresql.org Subject: Re: iterating over DictRow Message-ID: <20200925074820.GC2138@hermes.hilbert.loc> References: <20200923215444.GA25406@hermes.hilbert.loc> <20200924211147.GA2089@hermes.hilbert.loc> <9f9f7536-9d5f-d95b-b1eb-eed7ec1b83f6@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9f9f7536-9d5f-d95b-b1eb-eed7ec1b83f6@aklaver.com> Ma_X_il-Followup-to: d Re_X_turn-receipt-to: Karsten.Hilbert@gmx.net Di_X_sposition-Notification-To: Karsten.Hilbert@gmx.net X-Confi_X_rm-Reading-To: Karsten.Hilbert@gmx.net X-Pri_X_ority: 2 (High) Sender: X-Provags-ID: V03:K1:nlzhfvI2znLvNzhV5wgMPQe0Tsl3nJvRjqAfzYTrnBy2Fj8gbLg PFBr9NPXC9Pdb8YkvfZXD24ykbMGB36H3glorkkZ/0Fm5KOYDDNq9WsQNND2XSSJf/KBIOH YbSAxQ4p1IKXj3TytOp3zJPNDtoGgI/ygWzjNdm4FszEHDMwMosMxkERJbuB1IIuEMONWpU 2mhfex6sZ49bWSND0BQ3Q== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:cx1eFCbapIg=:yrAHmwAtW7iEPmy8vMY7yn LDoLoQmziIstdLOaMtF56SfcqV6I9CkWW4O8+Edi2vZw8L/T42ew6NKM3pTFLNEGlGQzDcw0p W8fzSPIIZauDwxVNdFcA/rCEYJRdg8dIq+eivh7tsZmHduH/s3JEoEF3JuY9y3VxJ0fs9Qhzl WcTrpW4EfkSmJ/n4EpbdtbA8FU+DrzewpiB/NZZYitRqNtmVi1ns8AQEYQZEIgWi8fN1keGtp WtWafC3etOobGgRbCG36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List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Adrian, thanks for tracing my misunderstanding. I should have confirmed my suspicion here: > https://github.com/psycopg/psycopg2/blob/fbba461052ae6ebc43167ab69ad91ca= db7914c83/lib/extras.py > class DictRow(list): > def __getitem__(self, x): > if not isinstance(x, (int, slice)): > x =3D self._index[x] > return super(DictRow, self).__getitem__(x) > > So if the value passed to __getitem__() is a integer or slice it does a = list > index. Indeed. I wonder whether that should be mentioned in the psycopg2 docs somewhere as it might be considered to violate the Principle Of Least Astonishment. Or rather, this issues seems unfortunate fallout from python3: In py2 one *had* to do DictRow.keys() to iterate over the keys. In py3 for key in DictRow: is the suggested idiom for that which, however, iterates over DictRow as a list (as it always did). DictRow.keys() still exists on dicts in py3 (and is not deprec(i?)ated to my knowledge) but now returns a memoryview (dict_keys, that is) rather than a list, which brings with it its own set of issues (dict and keys "list" are not independant objects anymore). So, neither using py2's for key in DictRow.keys(): under py3 nor changing to py3's for key in DictRow: # beep: variable wrongly named leads to fully equivalent code. So this is a py2/py3 Gotcha in psycopg2. Not that I complain, but worth a mention in the DictRow docs somewhere ? Karsten =2D- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B