{Notes to self, programming, technology, linux, windows, git} U {Papers, reviews, games, coffee, tabletennis, ramblings} = {things worth saving}

Monday, October 13, 2008

Python trick - getattr

getattr is teh awesome. Using string concatenation, we can obtain a real method reference (which is an object in python), and call it!

import printhelper #defines various print methods named "print_"

def print(to_print, format=):
to_call = getattr(printhelper, "print_%s" % type, printhelper.print_text)
return to_call(to_print)

No comments:

Followers