Python dict.get()
I am amazed how I did this mistake for 2 days in row.
>>> isinstance(a, dict) True >>> a.get('a', '')[:100] '1'
I thought it always returns a string ...
>>> a.get('b', '')[:100] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object has no attribute '__getitem__'
The dict is:
a = {'a': 1, 'b': None}
The proper way is:
>>> (a.get('b') or '')[:100] ''
Licensed under CC BY-SA 3.0