Self function

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Tue May 5 22:54:44 CEST 2009


Aaron Brady:
> >>> def auto( f ):
>
> ...     def _inner( *ar, **kw ):
> ...             return f( g, *ar, **kw )
> ...     g= _inner
> ...     return g

Looks nice, I'll try to the following variant to see if it's usable:

def thisfunc(fun):
    """Decorator to inject a default name of a
    function inside a function"""
    def _inner(*args, **kwds):
        return fun(g, *args, **kwds)
    g = _inner
    g.__name__ = fun.__name__
    g.__dict__.update(fun.__dict__)
    g.__doc__ = fun.__doc__
    g.__module__ = fun.__module__
    return g

@thisfunc
def SOMEVERYUGLYNAME(this, n):
    return 1 if n <= 1 else n * this(n - 1)

assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6

Bye,
bearophile



More information about the Python-list mailing list