[issue27933] functools.lru_cache seems to not work when renaming decorated functions

2016-09-01 Thread Федор Лянгузов

Федор Лянгузов added the comment:

Ok, thank you very much, i've got a little smarter today. Now i understand, 
that user_function (in this case factorial) is not modified by decorator, it is 
my job to change factorial to wrapper by assignment. Enlightning.

--
resolution:  -> not a bug
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27933] functools.lru_cache seems to not work when renaming decorated functions

2016-09-01 Thread Steven D'Aprano

Steven D'Aprano added the comment:

In case it isn't obvious, my example is meant to be pseudo-code, not the exact 
implementation used.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27933] functools.lru_cache seems to not work when renaming decorated functions

2016-09-01 Thread Steven D'Aprano

Steven D'Aprano added the comment:

This behaviour is expected. The factorial function calls itself, it doesn't 
call "f", but it is "f" which has the cache. So the call to f() goes through 
the cache, misses, and then calls factorial(), which has no cache.

In effect, what you have written is something like:

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

def f(n):
if n in f.cache:
return f.cache[n]
else:
x = f.cache[n] = factorial(n)
return x

f.cache = lru_cache()

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27933] functools.lru_cache seems to not work when renaming decorated functions

2016-09-01 Thread Emanuel Barry

Changes by Emanuel Barry :


--
nosy: +ncoghlan, rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27933] functools.lru_cache seems to not work when renaming decorated functions

2016-09-01 Thread Федор Лянгузов

New submission from Федор Лянгузов:

Greetings,

I've encountered strange behavior when using functools.lru_cache as a function 
(not as a decorator): it is at least miscounting misses, but probably not work 
at all, when the result of functools.lru_cache()(func) is saved in variable 
other than 'func'. Consider this snippet:

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

f = functools.lru_cache()(factorial)
f(20)
print(f.cache_info())

Output should be: CacheInfo(hits=0, misses=21, maxsize=128, currsize=21)
Instead it is: CacheInfo(hits=0, misses=1, maxsize=128, currsize=1)

I'm using Python 3.5.2 64bit on Windows 7 Professional 64bit.
I've written 3 unittests (using built-in module), which are attached as a file. 
I don't know how to comment them (conceptually, not syntactically), sorry.

Fedor

--
components: Library (Lib)
files: lru_cache_test.py
messages: 274149
nosy: Федор Лянгузов
priority: normal
severity: normal
status: open
title: functools.lru_cache seems to not work when renaming decorated functions
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file44325/lru_cache_test.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com