Follow

Things that feel like cheating in Python:

foo.get('not_here', {}).get('also_not_here')

The first get returns {} if the value isn't there, and then the second one won't fail by trying to run a get method on None.

· · Web · 2 · 3 · 0

@craigmaloney fyi: toolz provides a lot of functionality like this in a standardized way

for instance, toolz.dicttoolz.get_in

toolz.readthedocs.io/en/latest

@craigmaloney I do that pretty much in my work :D

But I think this is a bit ugly and there should be a better way of doing the same thing: it takes really too much space and characters to type.

@alex Considering I was thinking of using `if foo is None:`, I'm in hard agreement, but happy for the shortcut.

@craigmaloney ah, that's fine :) There is an syntactic construct I have never used for last year of python development:
```
somedict = {...}
if 'somekey' in somedict:
somevalue = somedict['somevalue']
```

I can always find an idiomatic way to make same thing simpler to read and faster to type (mostly using `dict.get`)
Sign in to participate in the conversation
Octodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!