Self function

Aaron Brady castironpi at gmail.com
Tue May 5 17:20:15 EDT 2009


On May 5, 3:54 pm, bearophileH... at lycos.com wrote:
> 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

/Actually/, the 'g' variable was spurious.  FWIW.

def auto( f ):
    def _inner( *ar, **kw ):
        return f( _inner, *ar, **kw )
    return _inner




More information about the Python-list mailing list