1. If the patching was done first it will work. The code needs to be changed to use the full import jsonify and then jsonify.jasonify = jasonify_mod 2. As the code stands, if/else is quicker. Quicker to load(less imports) and quicker to run. I will take your word for it that maybe there could be a performance improvement with generic functions in other cases... however my speed tests(see below) seem to point otherwise. Also note that if/else can translate easily into fast code(psyco or C). So if you do have a performance problem, if/else will be faster, or you could easily change it to use some other dispatch method. The if/else code with psyco runs 5.2 times faster, making it about 150 times faster than the generic dispatch version. When psyco is used it fails to run at all with generic functions. 3. You need to know what other rules exist with generic functions too. Or at least what order they are applied. Good point about only being able to put it at the top, unless you add extra code. 4. Interesting optimization. However generic functions are still slower in my tests. 5. Thanks for spotting the error! Ps. there are lots of warnings in your src/protocols/_speedups.c file ;) With the default debian compiler gcc4.02. For the case where you can not change the existing source, then generic functions seem to have a use. However there are other methods available which can work fine(arguably better). Maybe there are other use cases for them where they make more sense. However for the cases here, if/else works fine. Below is a version of jsonify with a speed test at the bottom. The if/else version is about 30 times faster. Not including the extra module load time. jsonify2.py is the if/else version, jsonify.py is the generic function version with the timing code attached to the bottom. Perhaps there could be a problem with my machine, so you can repeat the test on your own to check if you would like. time python2.4 jsonify.py Time for just the running parts. Not including module load time. 9.05578112602 real 0m9.659s user 0m9.428s sys 0m0.167s time python2.4 jsonify2.py Time for just the running parts. Not including module load time. 0.386996984482 real 0m0.805s user 0m0.721s sys 0m0.070s import sqlobject def jsonify(obj): # @@: Should this match all iterables? if isinstance(obj, (int, float, str, unicode)): return jsonify_simple(obj) elif isinstance(obj, (list, tuple)): return jsonify_list(obj) elif isinstance(obj, dict): return jsonify_dict(obj) elif isinstance(obj, sqlobject.SQLObject): return jsonify_sqlobject(obj) elif hasattr(obj, "__json__"): return jsonify_explicit(obj) elif isinstance(obj, sqlobject.SQLObject.SelectResultsClass): return jsonify_select_results(obj) def jsonify_simple(obj): return obj def jsonify_list(obj): return [jsonify(v) for v in obj] def jsonify_dict(obj): result = {} for name in obj: if not isinstance(name, (str, unicode)): raise ValueError( "Cannot represent dictionaries with non-string keys in JSON (found: %r)" % name) result[name] = jsonify(obj[name]) return result def jsonify_sqlobject(obj): result = {} result['id'] = obj.id for name in obj.sqlmeta.columns.keys(): result[name] = getattr(obj, name) return result def jsonify_explicit(obj): return obj.__json__() def jsonify_select_results(obj): return jsonify_list(obj) if __name__ == "__main__": import time a = range(100) b = {"asdf":2} class AA: def __json__(self): return "asdf" c = AA() t1 = time.time() for x in range(10000): r1 = jsonify(a) r2 = jsonify(b) r3 = jsonify(c) t2 = time.time() print "Time for just the running parts. Not including module load time. %s" % (t2 - t1)