Function decorator that caches function results

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Oct 9 08:21:59 EDT 2005


On Sun, 09 Oct 2005 13:43:38 +0200, Fredrik Lundh wrote:

> Steven D'Aprano wrote:
> 
>> I notice that type(some_closure) and type(ordinary_function) both return
>> the same result, <type 'function'>. I also notice that both the closure
>> and ordinary functions have an attribute "func_closure". Is there a way to
>> tell which is a closure and which is not? In my tests, the ordinary
>> function has func_closure == None, but I don't know if that will always be
>> the case of just for my tests.
> 
> closure != function.
> 
> (did you read the wikipedia page?  

Yes I did. Did you read my post?

If you create a closure, using a memoization technique as per the original
post, and then call type() on that closure, Python reports <type 'function'>. 

If you use dir() on that closure-that-Python-calls-a-function, it tells
you that there is an attribute "func_closure". But ordinary functions that
aren't closures also have that attribute.

According to my tests, ordinary functions have None as the func_closure
attribute, but I don't know if that will always be the case, or just the
few examples I tested it. With a sample size of three, I have no
confidence that I've found a bullet-proof test.


Other that that, thank you for the links.

-- 
Steven.




More information about the Python-list mailing list