I'd like to be able to apply some function fn to all arguments args arbitrarily i.e. regardless of the structure of the arguments. A recursive approach works.
def convert_args(args, fn):
if isinstance(args, tuple):
ret_list = [convert_args(i, fn) for i in args]
return tuple(ret_list)
elif isinstance(args, list):
return [convert_args(i, fn) for i in args]
elif isinstance(args, dict):
return {k: convert_args(v, fn) for k, v in args.items()}
return fn(args)
but I'm wondering how I could make this quicker. Are there any python tools which could help here, and if not, is this the kind of problem which lends itself well to the use of Cython/C extensions?