Convert std::set to frozenset #3836
Replies: 2 comments 6 replies
-
Update: actually I have prototyped something like this. I will test it out more and probably create a PR with this. For now feel free to use this sample in your code. I will be happy to hear some feedback! Here is my gist with answer: And results from tests: frozenset({1, 2, 3})
frozenset({1, 2, 3})
frozenset({5, 6, 7})
frozenset({5, 6, 7})
{frozenset({1, 2, 3}): 6}
{frozenset({1, 2, 3}): 6}
{1, 2, 3}
{1, 2, 3}
{5, 6, 7}
{5, 6, 7}
Traceback (most recent call last):
File "./test_frozenset.py", line 13, in <module>
print(mymodule.foo5({{1,2,3}: 6}))
TypeError: unhashable type: 'set' So to create However now there is an issue that pybind maintainers should answer: is adding new class based on top of already covered C++ type is something that breaks existing compatibility between versions? I guess I need to find some answers... 😃 |
Beta Was this translation helpful? Give feedback.
-
@jiwaszki If you want an example, Eigen::Ref has slightly different behavior for |
Beta Was this translation helpful? Give feedback.
-
I am trying to convert the type
std::map<std::set<int>, int>
from C++ to Python. All of these types have built-in conversions, but the conversion fails with the errorTypeError: unhashable type: 'set'
. This makes sense, as the built-in conversions are tying to create the invalid typeDict[Set[int], int]
in python. Is there any way I can get it to convert mystd::set
to a Pythonfrozenset
instead?frozenset
is hashable and can be used as map keys.EDIT: The conversion in the reverse direction is desirable as well. If I have a C++ function that takes a
std::map<std::set<int>, int>
I'd like to be able to bind it to Python and pass in aDict[FrozenSet[int], int]
.Beta Was this translation helpful? Give feedback.
All reactions