@lru_cache on functions with no arguments
Matt Wheeler
m at funkyhat.org
Tue Aug 1 09:14:35 EDT 2017
On Tue, 1 Aug 2017 at 12:53 Thomas Nyberg <tomuxiong at gmx.com> wrote:
> On 08/01/2017 01:06 PM, Matt Wheeler wrote:
> > A function which is moderately expensive to run, that will always return
> > the same result if run again in the same process, and which will not be
> > needed in every session.
> >
>
> What about just using a lazy getter property? E.g.:
>
> https://pypi.python.org/pypi/lazy-property
That doesn't necessarily solve any problems (Tom has already answered why
this won't work for Django, but also...)
- the methods then can't be passed around before being called, which will
make your lazy evaluation greedy again if that's why you're using the
lru_cache.
- this requires the functions to be methods of an instance of some class
(no, you can't use properties on a class, see below). That may be much more
overhead than the current solution (the object+class lookups plus @property
method call might add up to more overhead to a single object lookup &
lru_cache call as a rule anyway. I haven't investigated):
Python 3.6.0 (default, Mar 13 2017, 00:53:03)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... @property
... @classmethod
... def test(cls):
... return 'a'
...
>>> A.test
<property object at 0x1094afa98>
--
--
Matt Wheeler
http://funkyh.at
More information about the Python-list
mailing list