1.5.5 FunctionOrMethod

Quite often I find myself writing bytecodehacks that act uniformaly on methods and functions, so I wrote this class to relieve the tedium of checking all the time.

Use it like this:

def hack(ob):
    base = FunctionOrMethod(ob)
    func = base.function
    # manipulate func
    return base.make_function_or_method()

FunctionOrMethod (functionormethod)
If functionormethod is a method, make an InstanceMethod and assign it to self.method, then assign self.method.im_func to self.function.

On the other hand if functionormethod is a function, just make a Function out if it and whack it in self.function.

make_function_or_method ()
Can you guess what this function does?

is_method
1 if a method was passed to __init__, 0 otherwise. Can be useful if you need to slightly special-case methods.

function
A Function object. This is what you manipulate if you're using this class.

method
Only present if a method was passed to __init__. If present, it's what you think it is.

Send comments to mwh@python.net