Popping key causes dict derived from object to revert to object
Loris Bennett
loris.bennett at fu-berlin.de
Thu Mar 21 05:56:42 EDT 2024
Hi,
I am using SQLAlchemy to extract some rows from a table of 'events'.
>From the call to the DB I get a list of objects of the type
sqlalchemy.orm.state.InstanceState
I would like to print these rows to the terminal using the 'tabulate'
package, the documentation for which says
The module provides just one function, tabulate, which takes a list of
lists or another tabular data type as the first argument, and outputs
a nicely formatted plain-text table
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'>
<class 'sqlalchemy.orm.state.InstanceState'>
If I comment out the third line, which pops the unwanted key, I get
<class 'dict'>
<class 'dict'>
Why does popping one of the keys cause the elements of the list to
revert back to their original class?
Cheers,
Loris
--
This signature is currently under constuction.
More information about the Python-list
mailing list