Skip to content

Can supply a default return value to get()#95

Merged
calebcase merged 2 commits into
dpath-maintainers:masterfrom
AWhetter:fix_52
Jan 26, 2020
Merged

Can supply a default return value to get()#95
calebcase merged 2 commits into
dpath-maintainers:masterfrom
AWhetter:fix_52

Conversation

@AWhetter
Copy link
Copy Markdown
Contributor

This does not introduce any breaking changes.

Closes #52

@akesterson
Copy link
Copy Markdown
Collaborator

Why only add the functionality to get(), what about search()?

@akesterson
Copy link
Copy Markdown
Collaborator

Also, why not just catch KeyError and set the default value?

@AWhetter
Copy link
Copy Markdown
Contributor Author

AWhetter commented Dec 30, 2019

what about search()?

I'm on the fence but with search(). When searching you're potentially getting back an iterator and it feels weird to populate that with a default value. I haven't come across a use case where I've wanted a default value with search(), but I'm happy to add it if you think it would be useful?

why not just catch KeyError and set the default value?

You could definitely do that, but I think that this is a common enough case and catching an exception an "ugly" enough solution that it's worthwhile. .get() exists on a dictionary for this reason, even though you can catch the KeyError from using dict[].

@calebcase calebcase merged commit f463f86 into dpath-maintainers:master Jan 26, 2020
@calebcase
Copy link
Copy Markdown
Collaborator

Thanks for the contribution!

@akesterson
Copy link
Copy Markdown
Collaborator

Note that this was merged into 2.0, so you will need to pull a 2.x release to get this feature. (We are cutting the first 2.x release today)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Returning default value

3 participants