Created
October 21, 2012 01:48
-
-
Save n1ywb/3925430 to your computer and use it in GitHub Desktop.
Example use case for functools.partial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def render(self, request): | |
# Encapsulate the function call we wish to make into a partial object. | |
# Now if we change the function name or arguments we only change them | |
# in ONE place, instead of the two places this function is called from. | |
f = functools.partial(self.dlstatus.stn_to_json, self.id) | |
try: | |
request.setHeader("content-type", "application/json") | |
if request.args.has_key('callback'): | |
request.setHeader("content-type", "application/javascript") | |
return request.args['callback'][0] + '(' + | |
f() + ')' | |
return f() | |
except Exception, e: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment