Skip to content

Make lru_cache(maxsize=None) more discoverable #84751

Description

@rhettinger
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']

    Activity

    1. rhettinger commented on May 8, 2020

      @rhettinger
      ContributorAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on May 8, 2020
    3. rhettinger commented on May 8, 2020

      @rhettinger
      ContributorAuthor

      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.

    4. rhettinger commented on May 12, 2020

      @rhettinger
      ContributorAuthor

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

    5. transferred this issue fromon Apr 10, 2022
    6. tasty0tomato commented on Oct 25, 2024

      @tasty0tomato

      I know it is really really late now. But we should really add this change to What’s New In Python 3.9 because it is actually an interesting and important new feature.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions