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 1kLv5o-00009g-2d for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 21:17:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLv5H-0000EB-0w for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 21:16:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLv5G-0000E2-P6 for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 21:16:50 +0000 Received: from mout.gmx.net ([212.227.17.21]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLv59-0006Dl-Jt for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 21:16:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1601068598; bh=eVOtnWAcNwBlbiu5cxUSHPOArc8iir7VL6ym53SqiYY=; h=X-UI-Sender-Class:Date:From:To:Subject:References:In-Reply-To; b=KiYknYQmu0s8v31im5QrORZp4jn4+eCqNspN3zpJg5AG9DAFo09CVhdnwaFV0o9Jt Io6Sv/Ulv+8HIpVSogz+lyZdBZcdgOAwZMiexjEKNaYN6NS0H9Hycb4aXRAOidOlwW EIU7ggmFZUfCt5pZPvEoPVZyZD/S8/cd+9fWA1aI= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from hermes ([84.144.216.170]) by mail.gmx.com (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MVvLB-1jueup09HZ-00RnzG for ; Fri, 25 Sep 2020 23:16:38 +0200 Received: from ncq by hermes with local (Exim 4.94) (envelope-from ) id 1kLv51-0001gw-68 for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 23:16:35 +0200 Date: Fri, 25 Sep 2020 23:16:35 +0200 From: Karsten Hilbert To: psycopg@lists.postgresql.org Subject: Re: iterating over DictRow Message-ID: <20200925211635.GA1544@hermes.hilbert.loc> References: <20200923215444.GA25406@hermes.hilbert.loc> <20200924211147.GA2089@hermes.hilbert.loc> <9f9f7536-9d5f-d95b-b1eb-eed7ec1b83f6@aklaver.com> <20200925074820.GC2138@hermes.hilbert.loc> <4975719e-8981-c2ff-1408-c348730b31c5@aklaver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4975719e-8981-c2ff-1408-c348730b31c5@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:iGSl5f7AvkCITrJOTNxI1qtgx+f3izoZ/qFEbi+4sznQnhTgnsB 7qEFdJpUhLLhVvjWc3SqWVuLtmcqTHE3hmURgR0IbmakwyorBqE/a/9WI/NeCO3FQHd5gpU iPFBtp4z5BEsvzFsB5u1DIiIhMUaYkR+k/lAMJVONYSbwc32MV+OlEZ7MdqX0Kp+OEu0A7m UJQKlSdfyCSI+7MLn6XjQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:3820JFFTyRA=:wmAPTtETesKnECXUXWtItx 5nA/fZ/9mX4ECZQ71X0oCEi2lfClVDnSm+OTh6rAUqSFdHMfHeNf804G4k9Gi7Zw4/uWpkcVA 6cK0qcqeYi1KzvhAKc3l3bWeOGEhFEYKdhtBJ+IglrXI8wNP/guoJ3kXKGlP7DIlpPKmcOAuw yeRQhte1YudHa2lQmEvow9hGDD3YchrNsKe53DS9BxeE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MkfmD6el4vjlJayFGg== Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Sep 25, 2020 at 09:06:43AM -0700, Adrian Klaver wrote: > > 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. > > Well you can do, borrowing from previous example: > > for ky in r0._index: > print(ky) > > for ky in r0._index: > print(r0[ky]) > > Where _index is a substitute for *.keys(). Sure, there's a number of solutions to my immediate problem, the fitting of which is for key in dict(DictRow): That's the best fit because my def _escape_dict(the_dict, ...): was inaptly named. It should have been (and now is) def _escape_dict_like(dict_like, ...): within which dict(dict_like) is quite the thing to do despite having to make something a duck which already nearly quacks like one is somehwat unfortunate. Karsten =2D- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B