-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: add dict views #2358
types: add dict views #2358
Conversation
Thank you so much. Just a thought - not particularly necessary for me, would it be possible to add methods to E.g. |
src/types/dict.rs
Outdated
pub fn keys_view(&self) -> &PyDictKeys { | ||
unsafe { | ||
PyDictKeys::try_from_unchecked( | ||
self.call_method0(intern!(self.py(), "keys")).unwrap(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can self
be a dict subclass -- which can therefore return a different type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good point, this unchecked is not sound. I guess that would be the advantage of having a C API - then we could always hit the guaranteed pathway rather than lookup and allow subclasses to override things.
I think this is what I've called |
Is it safe to have a dictview while mutating the dict itself? |
Yes, looks like it: >>> d = {}
>>> k = d.keys()
>>> k
dict_keys([])
>>> d['foo'] = 1
>>> k
dict_keys(['foo']) |
9b25284
to
4ad66b5
Compare
I've pared this back to just be the |
ee5a692
to
c091318
Compare
For my use case, this looks great. Thanks so much. |
👍. Worth noting that it appears these types appear not to be supported on pypy, although they would probably be willing to add if requested on their issue tracker. |
Is there any chance that iterating over dict items will be faster than the list I get from |
Off the top of my head I'm not sure I can call it either way. Iterating lists is probably faster than iterating the keys collection, but you've got to pay the upfront cost of allocating and filling the whole list. It probably depends on dictionary size - as always, best to benchmark for your use case 😊 |
Inspired by #2347, having a play with adding the dictionary view types.
TODO
impl IntoIterator
?