Skip to content

Conversation

@CaerphillyMediaLtd
Copy link

I'm not sure how welcome this is, but we've added a dict-like get method to our codebase so that we can perform typical dictionary checking for keys, e.g.:

time = response.resources.get('lastTime', '01/01/2001')

Rather than having to check if lastTime exists, then setting a default if it doesn't. As with dict it's also very useful for stringing together values:

name = resources.get('person', {}).get('name', 'Unknown')

Which dramatically improves readability, and allows you to easily default to one value if one or both of person and name don't exist.

I think this is a much more pythonic way of handling it.

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

Successfully merging this pull request may close these issues.

1 participant