![]() Note, type specificity applies only to the function’s immediate arguments Types such as str and int may be cached separately even when typed Regard them as equivalent calls and only cache a single result. If typed is false, the implementation will usually If typed is set to true, function arguments of different types will beĬached separately. If maxsize is set to None, the LRU feature is disabled and the cache can count ( vowel ) for vowel in 'AEIOUaeiou' ) The maxsize at its default value of def count_vowels ( sentence ): return sum ( sentence. Lru_cache decorator to be applied directly to a user function, leaving If user_function is specified, it must be a callable. For example, f(a=1, b=2) and f(b=2, a=1)ĭiffer in their keyword argument order and may have two separate cache Since a dictionary is used to cache results, the positional and keywordĪrguments to the function must be hashable.ĭistinct argument patterns may be considered to be distinct calls with It is possible for the wrapped function to be called more than once ifĪnother thread makes an additional call before the initial call has been Remain coherent during concurrent updates. This means that the underlying data structure will The cache is threadsafe so that the wrapped function can be used in It can save time when an expensive or I/O boundįunction is periodically called with the same arguments. lru_cache ( maxsize = 128, typed = False )ĭecorator to wrap a function with a memoizing callable that saves up to the How do I cache method calls? for more details on how this differs from cached_property(). Stacking property() on top of lru_cache(). If a mutable mapping is not available or if space-efficient key sharing isĭesired, an effect similar to cached_property() can also be achieved by (as such classes don’t provide a _dict_ attribute at all). ![]() _slots_ without including _dict_ as one of the defined slots Read-only proxies for the class namespace), and those that specify ![]() Metaclasses (since the _dict_ attributes on type instances are This means it will not work with some types, such as This means that instance dictionariesĪlso, this decorator requires that the _dict_ attribute on each instanceīe a mutable mapping. Note, this decorator interferes with the operation of PEP 412 ThisĪllows the cached_property method to run again. The cached value can be cleared by deleting the attribute. Method and it works like a normal attribute. SubsequentĪttribute reads and writes take precedence over the cached_property When it does run, theĬached_property writes to the attribute with the same name. The cached_property decorator only runs on lookups and only when anĪttribute of the same name doesn’t exist. In contrast, a cached_property allows writes. A regular property blocks attribute writes unless a The mechanics of cached_property() are somewhat different from _data = tuple ( sequence_of_numbers ) def stdev ( self ): return statistics. Class DataSet : def _init_ ( self, sequence_of_numbers ): self.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |