You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What's the recommended way to iterate over PyMapping object?
#2718
I thought I could iterate over PyMapping object like this:
for(key, value)in mapping.iter()
But actually I cannot, and the below codes explain what is inside the PyMapping object:
fnfoo(d:&PyMapping){println!("d.iter() is PyResult<&PyIterator> type.");ifletOk(it) = d.iter(){println!("x is &PyIterator type.");for x in it{println!("x is Result<&PyAny, PyErr> type.");ifletOk(y) = x{println!("y is &PyAny type.");}}}}
So it seems that the iter method isn't something I am finding. Is there any suggestion?
Thanks very much!
Thanks for the answer. It seems that the items method is eager executed, and sometimes the for loop may need break in the middle, which does not require iterating over all the elements in the PyDict object. There might be some extra performance overhead?
By the way, I tried items().iter(), and still need to use multiple layers of if let to "extract" the element of the PyMapping object. So the further question could be:
In what kind of case, the iterating over PyMapping should return an PyErr? Shouldn't this data structure be always iterable?
In general we can't offer anything concrete methods in PyO3 which differ from this. However you could call the Python "items" method which might be support lazy iteration (e.g. for dict):
fnfoo(d:&PyMapping) -> PyResult<()>{for item in d.call_method0("items")?.iter()? {let(key, value) = item.extract()?;// do your thing}Ok(())}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
I thought I could iterate over
PyMappingobject like this:But actually I cannot, and the below codes explain what is inside the
PyMappingobject:So it seems that the
itermethod isn't something I am finding. Is there any suggestion?Thanks very much!
All reactions