getting the current function

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sun Sep 9 10:05:17 CEST 2007


In message <mailman.229.1189182388.2658.python-list at python.org>, Gary
Robinson wrote:

> I've just never liked the fact that you have to name the function when
> accessing those attributes from within the function.

If it's any consolation, it's not actually the function name you need to
refer to, merely any variable or Python object that happens to hold a
reference to the right function:

    >>> f = lambda x : x + x
    >>> f(2)
    4
    >>> f.myattr = "yeah"
    >>> g = f
    >>> g.myattr
    'yeah'
    >>> g = [f]
    >>> g[0].myattr
    'yeah'
    >>> g[0](3)
    6




More information about the Python-list mailing list