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

Option to make the lru_cache type specific #57436

rhettinger opened this issue Oct 19, 2011 · 8 comments

Option to make the lru_cache type specific #57436

rhettinger opened this issue Oct 19, 2011 · 8 comments
stdlib Python modules in the Lib dir type-feature A feature request or enhancement


Copy link

BPO 13227
Nosy @rhettinger, @ncoghlan, @vstinner, @ezio-melotti
  • typed_lru.diff: Add type differentiation to the cache entries
  • typed_lru2.diff: Patch with tests and application to re module
  • typed_lru3.diff: Updated patch with docs
  • 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 = ''
    closed_at = <Date 2011-10-20.15:59:28.817>
    created_at = <Date 2011-10-19.22:06:36.315>
    labels = ['type-feature', 'library']
    title = 'Option to make the lru_cache type specific'
    updated_at = <Date 2011-10-20.15:59:28.815>
    user = '' fields:

    activity = <Date 2011-10-20.15:59:28.815>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2011-10-20.15:59:28.817>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2011-10-19.22:06:36.315>
    creator = 'rhettinger'
    dependencies = []
    files = ['23473', '23475', '23480']
    hgrepos = []
    issue_num = 13227
    keywords = ['patch']
    message_count = 8.0
    messages = ['145976', '145985', '145990', '145994', '146007', '146008', '146022', '146023']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'ncoghlan', 'vstinner', 'ezio.melotti', 'python-dev']
    pr_nums = []
    priority = 'low'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = ''
    versions = ['Python 3.3']

    Copy link
    Contributor Author

    Based on a comment from Andrew Koenig, it is suggested that the lru_cache() offer an option to become type specific so that equal objects of different typed don't get cached to the same entry.

    Here's an example:

    def square(x):
        print('squaring', x)
        return x * x
    for x in [3, 3.0, 3, 3.0]:

    Without type specificity, all four calls will return 9 rather than 9. 9.0, 9, 9.0.

    First-pass patch attached.

    @rhettinger rhettinger self-assigned this Oct 19, 2011
    @rhettinger rhettinger added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Oct 19, 2011
    Copy link
    Contributor Author

    Updated patch to include tests and an application to the re module.

    Copy link

    The patch doesn't update the doc. You may also test type(square(3)). Except of these nits, the patch looks good.

    The typed option is an important feature, and your patch contains a good example of its usage (the re module).

    Copy link

    Features looks good, but the current patch doesn't handle keyword arguments correctly (more details in the Reitveld review).

    Copy link
    Contributor Author

    Nick, Ezio, and Victor, thanks for looking at the code.
    Here's an updated the patch:

    • optimization for fast globals() are now keyword-only arguments
    • now types the keyword values, not the key/values tuples
    • expanded tests
    • added the doc update

    Copy link

    Looks good to me (although it took me longer than it should have to figure out why you didn't need to store the keyword argument names a second time)

    Copy link

    python-dev mannequin commented Oct 20, 2011

    New changeset cba503a2c468 by Raymond Hettinger in branch 'default':
    bpo-13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).

    Copy link
    Contributor Author

    Victor, Ezio, and Nick, thanks for the review.
    Andrew, thanks for the feature request.

    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    None yet

    No branches or pull requests

    3 participants