Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: [Python-ideas] Using functools.lru_cache only on some arguments of a function Date: Fri, 4 Dec 2015 15:07:26 -0700 Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de g7dTGkPAabUtNu+MuOGW7g7JRU0/dJClVkSPKaY//l0g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:: [': 0.03; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'wrapper': 0.07; 'argument,': 0.09; 'calculating': 0.09; 'output,': 0.09; 'subject:skip:f 10': 0.09; 'def': 0.13; 'ignore': 0.14; 'subject: \n ': 0.15; 'expense.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:ideas': 0.16; 'wrote:': 0.16; 'result,': 0.18; 'subject:] ': 0.19; '2015': 0.20; '(or': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'calculated': 0.29; 'subject:some': 0.29; 'code:': 0.29; 'summary': 0.29; "i'm": 0.30; 'bill': 0.32; 'skip:d 40': 0.32; 'this?': 0.34; 'skip:d 20': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'received:209': 0.38; 'skip:p 20': 0.38; 'does': 0.39; 'takes': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'some': 0.40; 'real': 0.62; 'here': 0.66; 'results': 0.66; 'url:r': 0.67; 'obvious': 0.76; 'posed': 0.84; 'quicker': 0.84; 'subject:Using': 0.84; 'to:name:python': 0.84; 'url:comments': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Jke13Cq9p/5PjHqzSXkRVRG7048Ki1MlYsvsHRzoYKI=; b=XOoaWIXlA2nQ5pFFoDKFxUbaGnDfVh0lH4coR9d9G1DwlW9Ibn+vDoLRuDwedqpe1R 7KQQXC5Zi92YSuH1Vl9zT3/b1IgBvyV1/GBZLIbl7+cAhj853bU9/kSOsY1D5I0P8CA/ TnzkxKOxBE2k8K4w3cd8oal3O2gNboWTqNuIldKb71i0Ob4tiTUBn0DLm+Crn/hjykLO Tcdwk3CaaTjubnrAVmMpafBKAZOWjsoYVqqzo4vO+HLX0a6uMqxagncg4fKbxh/+cgFs COV2dCdxWtqoto2UAF6qXjlwqzutZZU5tei5q3jELd/85UDzsgxih1hmbC873tQDOge8 nXRg== X-Received: by 10.107.19.12 with SMTP id b12mr18865996ioj.11.1449266886295; Fri, 04 Dec 2015 14:08:06 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100012 On Fri, Dec 4, 2015 at 2:44 PM, Bill Winslow wrote: > This is a question I posed to reddit, with no real resolution: > https://www.reddit.com/r/learnpython/comments/3v75g4/using_functoolslru_cache_only_on_some_arguments/ > > The summary for people here is the following: > > Here's a pattern I'm using for my code: > > def deterministic_recursive_calculation(input, partial_state=None): > condition = do_some_calculations(input) > if condition: > return deterministic_recursive_calculation(reduced_input, > some_state) > > Basically, in calculating the results of the subproblem, the subproblem can > be calculated quicker by including/sharing some partial results from the > superproblem. (Calling the subproblem without the partial state still gives > the same result, but takes substantially longer.) > > I want to memoize this function for obvious reasons, but I need the > lru_cache to ignore the partial_state argument, for its value does not > affect the output, only the computation expense. > > Is there any reasonable way to do this? What form does the partial_state take? Would it be reasonable to design it with __eq__ and __hash__ methods so that each partial state (or a wrapper of it) is considered equal?