Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make lru_cache(maxsize=None) more discoverable #84751

Closed
rhettinger opened this issue May 8, 2020 · 3 comments
Closed

Make lru_cache(maxsize=None) more discoverable #84751

rhettinger opened this issue May 8, 2020 · 3 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 40571
Nosy @rhettinger
PRs
  • bpo-40571: Make lru_cache(maxsize=None) more discoverable #20019
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-05-12.00:01:23.494>
    created_at = <Date 2020-05-08.21:22:40.410>
    labels = ['type-feature', 'library', '3.9']
    title = 'Make lru_cache(maxsize=None) more discoverable'
    updated_at = <Date 2020-05-12.00:01:23.493>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2020-05-12.00:01:23.493>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-12.00:01:23.494>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-05-08.21:22:40.410>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40571
    keywords = ['patch']
    message_count = 3.0
    messages = ['368469', '368471', '368684']
    nosy_count = 1.0
    nosy_names = ['rhettinger']
    pr_nums = ['20019']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40571'
    versions = ['Python 3.9']

    @rhettinger
    Copy link
    Contributor Author

    The recent discussions on python-ideas showed that people have a hard time finding the infinity-cache option for lru_cache(). Also, in the context of straight caching without limits, the name *lru_cache()* makes the tool seem complex and heavy when in fact, it is simple, lightweight, and fast (doing no more than a simple dictionary lookup).

    We could easily solve both problems with a helper function:

        def cache(func):
            'Simple unbounded cache.  Sometimes called "memoize".'
            return lru_cache(maxsize=None, typed=False)

    It would be used like this:

         @cache
         def configure_server():
             ...
             return server_instance

    There was some discussion about a completely new decorator with different semantics (holding a lock across a call to an arbitrary user function and being limited to zero argument functions). It all the examples that were presented, this @cache decorator would suffice. None of examples presented actually locking behavior.

    @rhettinger rhettinger added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 8, 2020
    @rhettinger
    Copy link
    Contributor Author

    FWIW, this doesn't preclude the other proposal if it ever gains traction and moves forward. This just takes existing functionality and improves clarity and discoverability.

    The core issue is that if you only want a simple unbounded cache, it isn't obvious that that behavior is buried in the lru_cache() API. In hindsight, this always should have been separate functionality. And some day we may deprecate the maxsize=None option which is somewhat opaque.

    @rhettinger
    Copy link
    Contributor Author

    New changeset 21cdb71 by Raymond Hettinger in branch 'master':
    bpo-40571: Make lru_cache(maxsize=None) more discoverable (GH-20019)
    21cdb71

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant