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

missing haskey, get, etcetera? #148

Closed
stevengj opened this issue Apr 12, 2022 · 3 comments
Closed

missing haskey, get, etcetera? #148

stevengj opened this issue Apr 12, 2022 · 3 comments

Comments

@stevengj
Copy link
Member

stevengj commented Apr 12, 2022

If I have a Python dictionary object or similar:

julia> d = pyeval("{'foo': 1, 'bar': 2}", Main)
Python dict: {'foo': 1, 'bar': 2}

it would be nice to be able to use haskey(d, x) (by checking whether PyObject_GetItem returns NULL), get(d, x, default) (similar to this), etcetera.

Maybe call it pyhasitem if you don't want to overload haskey. You already have a 2-arg pygetitem, so you could add a 3-arg version instead of overloading get. (Though I'm not sure what's wrong with overloading haskey and get, since you already overload getindex.)

@stevengj
Copy link
Member Author

It works to wrap d in PyDict{Any,Any}(d), of course.

@cjdoris
Copy link
Collaborator

cjdoris commented Apr 21, 2022

OK I've added pyhasitem, 3-arg pygetitem. I'm a little wary of this, as I try to stick to the Python API and these don't exist, but they are logical analogues of pyhasattr and pyhasitem.

Have used them to implement get, get! and haskey.

@cjdoris cjdoris closed this as completed Apr 21, 2022
@stevengj
Copy link
Member Author

Thanks !

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

2 participants