python - How to determine if a non-empty string exists in a dictionary? -


i find myself checking dictionaries in python see if key in dictionary , if key has useful information (in case, nonzero-length string). instance, seem need check kwargs data this:

if ('partition' not in kwargs or kwargs['partition'] none     or kwargs['partition'] == ""):     raise exception("need 'partition' method") 

is there simpler, straightforward way missing?

use dictionary object's get() method , let python determine truthiness automatically:

if not d.get('partition'):     raise exception("need 'partition' method") 

Comments