Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100012

Re: [Python-ideas] Using functools.lru_cache only on some arguments of a function

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: [Python-ideas] Using functools.lru_cache only on some arguments of a function
Date 2015-12-04 15:07 -0700
Message-ID <mailman.204.1449266895.14615.python-list@python.org> (permalink)
References <CAAWJOpayZ09YNHxi4FfZVES1bUrb3EX3OXeiCjZwxyyKqy=Xtw@mail.gmail.com>

Show all headers | View raw


On Fri, Dec 4, 2015 at 2:44 PM, Bill Winslow <bunslow@gmail.com> 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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: [Python-ideas] Using functools.lru_cache only on some arguments of a function Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-04 15:07 -0700

csiph-web