Skip to content

Iterate over enums #253

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

Closed
romainthomas opened this issue Jun 27, 2016 · 5 comments
Closed

Iterate over enums #253

romainthomas opened this issue Jun 27, 2016 · 5 comments

Comments

@romainthomas
Copy link

Actually we can't iterate over a enum. It could be nice to enable this kind of code:

for e in myEnum:
    print e
@wjakob
Copy link
Member

wjakob commented Jun 27, 2016

Are you proposing to implement such a feature? A PR with a custom __iter__ handler would be welcomed.

@aldanor
Copy link
Member

aldanor commented Jun 27, 2016

A side thought, would it make sense to consider using standard library's enum inside of rolling our own? (only in Python 3, but there's an official backport to Python 2). Then you would get all kinds of goodies like iteration etc for free.

https://docs.python.org/3/library/enum.html -- IntEnum, to be more specific

@wjakob
Copy link
Member

wjakob commented Jun 27, 2016

It sounds tricky -- Enum is a kind of magic class in Python3, and it requires installing an extra library on python2. I think duck typing is the way to go here.

@romainthomas
Copy link
Author

Are you proposing to implement such a feature? A PR with a custom iter handler would be welcomed.

@wjakob I will try.

Actually I use this trick:

import re
def enum_to_dict(enum):
    return {k: v for k, v in enum.__dict__.iteritems() if not re.match("__(.*)__", str(k))}

@wjakob
Copy link
Member

wjakob commented Jun 28, 2016

I'll close this ticket since there isn't an issue with pybind11 -- if you'd like to contribute this as a feature, please open a pull request.

Thanks,
Wenzel

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

No branches or pull requests

3 participants