4 ms·
They added a memoize decorator: https://docs.python.org/3.9/library/functools.html#functools.cache https://docs.python.org/3.9/library/functools.html#functools.
by uyt 6y ago
They added a memoize decorator: https://docs.python.org/3.9/library/functools.html#functools.cache https://docs.python.org/3.9/library/functools.html#functools...
Pretty useful for DP interview questions when implementing top down.
- codethief 6y agoI find it weird that they called it `cache` instead of `cached` which would be more in line with `cached_property`. One of my colleagues already tripped over this today when he told me about this new feature.
- bobbylarrybobby 6y agoPython has a lot of weird naming choices. Heck, they didn’t even Putin underscore in their new string method `removeprefix`
- dec0dedab0de 6y agoWell one is a type of property and the other is saying cache what I'm decorating
- delaaxe 6y agoThey wanted to match `@functools.lru_cache`
- rhencke 6y agoWhile nice, this isn't new. This is just a convenience for 'lru_cache(maxsize=None)'. lru_cache has been supported since Python 3.2. See: https://docs.python.org/3.2/library/functools.html#functools.lru_cache https://docs.python.org/3.2/library/functools.html#functools...
- nneonneo 6y agoThey do mention that it’s more efficient than lru_cache, which isn’t surprising since cache is a lot simpler (just throw all the values in a dict keyed by the args).
- uyt 6y agoNo they are saying it's more efficiently that lru_cache with a finite maxsize. Implementation-wise it's identical to using maxsize=None. See source: https://github.com/python/cpython/blob/3.9/Lib/functools.py#L652 https://github.com/python/cpython/blob/3.9/Lib/functools.py#...
- dralley 6y agoIt says it's more efficient than lru_cache with a size specified, it doesn't say that it's more efficient than lru_cache without one.
- DonaldPShimoda 6y agoOh sure, now they implement it, now that I've written my own so many times... (This is a great addition and I'm very excited for it. Just a little sore haha.)
- ben-schaaf 6y agoAs others have stated, functools.lru_cache has existed for quite some time.
- DonaldPShimoda 6y agoNever realized you could turn off the maximum size in the `lru_cache`, actually. Maybe I'll have to read those docs more carefully haha.
- kamaal 6y agoThey won't let you use it in the interviews.