Nov-26-2019, 12:51 PM
Then catch two exceptions:
You expect that the value is also a dict. But it seems, that sometimes there is a
try: # code except (KeyError, TypeError): # code to handle the exceptionThe
TypeError
came, because test
or aa
returns None.You expect that the value is also a dict. But it seems, that sometimes there is a
None
.example_dict = { 'dict': { 'subdict': { 'subsubdict': { 'value': 42, } } } } print(example_dict['dict']['subdict']['subsubdict']['value']) # now replacing the value of subdict with None. example_dict['dict']['subdict']['subsubdict'] = None # TypeError print(example_dict['dict']['subdict']['subsubdict']['value'])
Error:---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-0a16fb6ffb69> in <module>
----> 1 print(example_dict['dict']['subdict']['subsubdict']['value'])
TypeError: 'NoneType' object is not subscriptable
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!