[New-bugs-announce] [issue33827] Generators with lru_cache can be non-intuituve

Michel Albert report at bugs.python.org
Mon Jun 11 03:16:13 EDT 2018


New submission from Michel Albert <exhuma at gmail.com>:

Consider the following code:

    # filename: foo.py

    from functools import lru_cache


    @lru_cache(10)
    def bar():
        yield 10
        yield 20
        yield 30


    # This loop will work as expected
    for row in bar():
        print(row)

    # This loop will not loop over anything.
    # The cache will return an already consumed generator.
    for row in bar():
        print(row)


This behaviour is natural, but it is almost invisible to the caller of "foo".

The main issue is one of "surprise". When inspecting the output of "foo" it is clear that the output is a generator:

    >>> import foo
    >>> foo.bar()
    <generator object bar at 0x7fbfecb66a40>

**Very** careful inspection will reveal that each call will return the same generator instance.

So to an observant user the following is an expected behaviour:

    >>> result = foo.bar()
    >>> for row in result:
    ...    print(row)
    ...
    10
    20
    30
    >>> for row in result:
    ...     print(row)
    ...
    >>>

However, the following is not:

    >>> import foo
    >>> result = foo.bar()
    >>> for row in result:
    ...     print(row)
    ...
    10
    20
    30
    >>> result = foo.bar()
    >>> for row in result:
    ...     print(row)
    ...
    >>>


Would it make sense to emit a warning (or even raise an exception) in `lru_cache` if the return value of the cached function is a generator?

I can think of situation where it makes sense to combine the two. For example the situation I am currently in:

I have a piece of code which loops several times over the same SNMP table. Having a generator makes the application far more responsive. And having the cache makes it even faster on subsequent calls. But the gain I get from the cache is bigger than the gain from the generator. So I would be okay with converting the result to a list before storing it in the cache.

What is your opinion on this issue? Would it make sense to add a warning?

----------
messages: 319279
nosy: exhuma
priority: normal
severity: normal
status: open
title: Generators with lru_cache can be non-intuituve
type: behavior

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33827>
_______________________________________


More information about the New-bugs-announce mailing list