Skip to content
Advertisement

Tag: functools

@lru_cache decorator excessive cache misses

How can you configure lru_cache to key its cache based on actual values received, rather than how the function was called? In other words, only the first call above should be a cache miss, the other two should be cache hits. Answer To do that, you would have to go through the process of binding arguments to formal parameters. The

Does python functools.singledispatch work with Generator type?

I extended the example at https://docs.python.org/3/library/functools.html#functools.singledispatch by adding a registration for generator type while it works with list, it doesn’t seem to work with generator with error like Is it expected that singledispatch does not work with generator? Answer typing.Generator is a type hint, not a type. You need types.GeneratorType. Objects are not considered instances of type hints according to

Advertisement