Skip to content
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

Add pybind11::make_key_iterator for map iteration #282

Merged
merged 1 commit into from
Aug 12, 2016

Conversation

jagerman
Copy link
Member

@jagerman jagerman commented Jul 15, 2016

This allows exposing a dict-like interface to python code, allowing iteration over keys via:

for k in custommapping:
    ...

It does this via a new function, rather than overloading py::make_iterator for pair containers, so that you can still expose iteration over pairs, allowing you to also provide a dict.items()-compatible functionality, allowing:

for k, v in custommapping.items():
    ...

by defining an __iter__ method that returns pybind11::make_key_iterator and a items method that returns pybind11::make_iterator.

example6 is updated with an example class providing both types of iteration.

@jagerman
Copy link
Member Author

I didn't notice PR #235 before I submitted this, but I think this key iterator would be useful there, too (and, of course, there is a bit of overlap as both add the KeyError exception wrapper).

@wjakob
Copy link
Member

wjakob commented Aug 11, 2016

I'd like to merge this fairly soon -- can you rebase it onto head whenever you have a moment?

This allows exposing a dict-like interface to python code, allowing
iteration over keys via:

    for k in custommapping:
        ...

while still allowing iteration over pairs, so that you can also
implement 'dict.items()' functionality which returns a pair iterator,
allowing:

    for k, v in custommapping.items():
        ...

example-sequences-and-iterators is updated with a custom class providing
both types of iteration.
@jagerman
Copy link
Member Author

Done.

@wjakob
Copy link
Member

wjakob commented Aug 12, 2016

Great, thanks!

@wjakob wjakob merged commit 09f40e0 into pybind:master Aug 12, 2016
@jagerman jagerman deleted the key-iterators branch August 15, 2016 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants