Re: Popping key causes dict derived from object to revert to object

Liste des GroupesRevenir à cl python 
Sujet : Re: Popping key causes dict derived from object to revert to object
De : dieter.maurer (at) *nospam* online.de
Groupes : comp.lang.python
Date : 21. Mar 2024, 18:21:52
Autres entêtes
Message-ID : <mailman.0.1711038451.3468.python-list@python.org>
References : 1 2
User-Agent : VM 8.0.12-devo-585 under 21.4 (patch 24) "Standard C" XEmacs Lucid (x86_64-linux-gnu)
Loris Bennett wrote at 2024-3-21 10:56 +0100:
...
So as I understand it, I need to convert the InstanceState-objects to,
say, dicts, in order to print them.  However I also want to remove one
of the keys from the output and assumed I could just pop it off each
event dict, thus:
>
   event_dicts = [vars(e) for e in events]
   print(type(event_dicts[0]))
   event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
   print(type(event_dicts[0]))
>
However, this prints
>
 <class 'dict'>

`vars` typically returns a `dict`.

 <class 'sqlalchemy.orm.state.InstanceState'>
This is what you have popped.
>
If I comment out the third line, which pops the unwanted key, I get
Then you do not change `event_dicts`.

You problem likely is:
`pop` does not return the `dict` after the removal of a key
but the removed value.

Date Sujet#  Auteur
21 Mar 24 * Popping key causes dict derived from object to revert to object14Loris Bennett
21 Mar 24 +- Re: Popping key causes dict derived from object to revert to object1dieter.maurer
21 Mar 24 `* Re: Popping key causes dict derived from object to revert to object12Mark Bourne
22 Mar 24  `* Re: Popping key causes dict derived from object to revert to object11Loris Bennett
22 Mar 24   +* Re: Popping key causes dict derived from object to revert to object7Grant Edwards
25 Mar 24   i`* Re: Popping key causes dict derived from object to revert to object6Loris Bennett
25 Mar 24   i +* Re: Popping key causes dict derived from object to revert to object3Michael F. Stemper
25 Mar 24   i i`* Re: Popping key causes dict derived from object to revert to object2Loris Bennett
25 Mar 24   i i `- Re: Popping key causes dict derived from object to revert to object1Jon Ribbens
25 Mar 24   i +- Re: Popping key causes dict derived from object to revert to object1Grant Edwards
25 Mar 24   i `- Re: Popping key causes dict derived from object to revert to object1<avi.e.gross
22 Mar 24   +- Re: Popping key causes dict derived from object to revert to object1<avi.e.gross
25 Mar 24   +- Re: Popping key causes dict derived from object to revert to object1Loris Bennett
25 Mar 24   `- Re: Popping key causes dict derived from object to revert to object1<avi.e.gross

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal