I appreciate that point as it is what I must be misunderstanding. I believe the performance speed up of [((f(x) as h), g(h)) for x range(10)] is that there are 10 calls to compute f, not 20.
You can do this with a dictionary right now, at least for the example we're talking about: [(d[x], g(d[x])) for x in range(10) if d.update({x:f(x)}) is None] It's ugly but get's the job done. The proposed syntax is leagues better than that, but just to give my point a concrete example. If f is memoized, isn't [( f(x), g(f(x)) ) for range(10)] the same? You compute f 10 times, not 20. You get the second f(x) as cache retrieval instead of recomputing it, precisely because the argument x is the same. Here I can use 'd' later if needed, as opposed to with the proposal. That's really my point about memoization (or cacheing). If 'f' is really expensive, I don't really see the point of using an ad-hoc local caching of the value that lives just for one statement when I could use it where-ever, even persist it if it makes sense. I fully admit I'm at my depth here, so I can comfortably concede it's better than memoization and I just don't understand! On Wed, Feb 28, 2018 at 12:37 AM, Chris Angelico <ros...@gmail.com> wrote: > On Wed, Feb 28, 2018 at 6:46 PM, Matt Arcidy <marc...@gmail.com> wrote: >> I have been struggling to justify the need based on what I have read. I >> hope this isn't a dupe, I only saw caching mentioned in passing. >> >> Also please excuse some of the naive generalizations below for illustrative >> purposes. >> >> Is there a reason memoization doesn't work? If f is truly expensive, using >> this syntax only makes sense if you have few comprehensions (as opposed to >> many iterations) and few other calls to f. Calling f in 10 comprehensions >> would (naively) benefit from memoization more than this. It appears to me >> to be ad-hoc memoization with limited scope. is this a fair statement? > > Memoization is only an option if the expression in question is (a) a > single cacheable function call, and (b) used twice without any > variation. If it's any sort of more complicated expression, that > concept doesn't work. > > ChrisA > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/