Is there a way to change the closure of a python function?

ast nomail at com.invalid
Wed Sep 28 03:08:41 EDT 2016


"jmp" <jeanmichel at sequans.com> a écrit dans le message de 
news:mailman.31.1474987306.2302.python-list at python.org...
> On 09/27/2016 04:01 PM, Peng Yu wrote:

>
> Note: function are objects, and can have attributes, however I rarely see usage of these, there 
> could be good reasons for that.
>

It could be use to implement a cache for example.

def fact1(n):

    if not hasattr(fact1, '_cache'):
        fact1._cache = {0:1}

    if n not in fact1._cache:
        fact1._cache[n] = n * fact1(n-1)

    return fact1._cache[n] 




More information about the Python-list mailing list