Skip to content

Instantly share code, notes, and snippets.

@MichaelBlume
Created May 23, 2012 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelBlume/2776725 to your computer and use it in GitHub Desktop.
Save MichaelBlume/2776725 to your computer and use it in GitHub Desktop.
The dumps param in simplejson.dumps is often helpful, but sometimes you want to quickly pass in multiple default actions. This should help.
def compose_defaults(*defaults):
'''I got annoyed that simplejson.dumps doesn't let you pass multiple
defaults. So here's this.'''
def new_default(obj):
for default in defaults:
try:
return default(obj)
except:
pass
raise TypeError("no default functions succeeded")
return new_default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment