Skip to content

Instantly share code, notes, and snippets.

@decibel
Created April 5, 2016 23:35
Show Gist options
  • Save decibel/b3d7ea5883c73cd3daf0abd56cc272d1 to your computer and use it in GitHub Desktop.
Save decibel/b3d7ea5883c73cd3daf0abd56cc272d1 to your computer and use it in GitHub Desktop.
Simple python function to return a default value depending on the data type
def clear(i):
if isinstance(i, basestring):
return ''
elif isinstance(i, int):
return 0
elif isinstance(i, float):
return 0.0
elif i is None:
return None
elif isinstance(i, dict):
return {k: clear(v) for k,v in i.iteritems()}
elif isinstance(i, list):
return [clear(item) for item in i]
raise TypeError('unknown type ' + str(type(i)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment