interface boilerplate

John Hunter jdhunter at ace.bsd.uchicago.edu
Mon Oct 18 09:29:55 EDT 2004


>>>>> "Alex" == Alex Martelli <aleaxit at yahoo.com> writes:

    Alex> To make a function just like another but with a different
    Alex> name:

    Alex> def changed_name_function(f, newname): import new return
    Alex> new.function(f.func_code, f.func_globals, newname,
    Alex> f.func_defaults, f.func_closure)

    Alex> I believe this should work in 2.2 as well (not tested).

I tested this - the signature of new.function in 2.2 is a bit
different

function(...)
    Create a function object from (CODE, GLOBALS, [NAME [, ARGDEFS]]).

so it doesn't take the 5 arg version posted.  

I am having a little trouble figuring out how to handle the call
signature for 2.2.  I tried this modification (matplotlib._python23 is
a flag that returns True iff python version >=2.3


def changed_name_function(f, newname):
    import new
    if matplotlib._python23:
        newf =  new.function(f.func_code, f.func_globals, newname,
                             f.func_defaults, f.func_closure)
    else:
        if f.func_defaults is None:
            argdefs = ()
        else:
            argdefs = f.func_defaults
        newf =  new.function(f.func_code, f.func_globals, newname,
                             argdefs)

    newf.__doc__ = f.__doc__
    return newf

I added the None check on f.func_defaults because I was getting the
error

  TypeError: function() argument 4 must be tuple, not None

But this does not appear to be right either because I get a segfault
:-(  Note that the suggestion works as advertised for python2.3.

Any ideas?

Thanks,
John Hunter



More information about the Python-list mailing list