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
You can check for existence of the key with obj.contains:
if (kwargs.contains("switch")) {
// ...
}
and for the value equaling none with obj.is_none():
if (!kwargs["switch"].is_none()) {
// ...
}
But note that, like Python, key existence and value equaling None are different: it is possible for a dictionary to contain the key "switch" with a value of None, which is different than the dictionary not containing the key. So you may actually want:
if (kwargs.contains("switch") && !kwargs["switch"].is_none()) {
// ...
}
It's also possible to do a None check using !val.is(py::none()) rather than !val.is_none(), but it's slightly more verbose, and slightly less efficient.
Activity
jagerman commentedon Mar 29, 2018
You can check for existence of the key with
obj.contains:and for the value equaling none with
obj.is_none():But note that, like Python, key existence and value equaling None are different: it is possible for a dictionary to contain the key "switch" with a value of
None, which is different than the dictionary not containing the key. So you may actually want:It's also possible to do a
Nonecheck using!val.is(py::none())rather than!val.is_none(), but it's slightly more verbose, and slightly less efficient.ecolss commentedon May 30, 2018
@jagerman what if I want the key to be integer, not string, looks like it cannot compile at all
cielavenir commentedon Jan 30, 2024
@ecolss @jagerman it should be addressed by #4004 . I don't know why it is taking time.
cielavenir commentedon Jan 30, 2024
though you can use
dic[py::cast(num)]