I want to memoize including Python keyword arguments

The Pythonwiki introduces the following memoize decorators:

# note that this decorator ignores **kwargs
def memoize(obj):
    cache = obj.cache = {}

    @functools.wraps(obj)
    def memoizer(*args, **kwargs):
        if args not in cache:
            cache[args] = obj(*args, **kwargs)
        return cache[args]
    return memoizer

However, as stated in the comment, it seems that it does not support kwargs. Let's deal with it.

# do not use "self" for a name of argument.
import inspect
def memoize(obj):
    cache = obj.cache = {}

    @functools.wraps(obj)
    def memoizer(*args, **kwargs):
        argdict = inspect.getcallargs(obj, *args, **kwargs)
        argdict.pop('self', None) # if obj is a bound method, arguments includes "self"
        argset = frozenset(argdict.iteritems()) # for Python3, use dict.items() instead
        if argset not in cache:
            cache[argset] = obj(*args, **kwargs)
        return cache[argset]
    return memoizer

The point is to use the standard library inspect. inspect.getcallargs ()

>>> def f(a, b, c=10):
...     pass
... 
>>> inspect.getcallargs(f, 1, 2) 
{'a': 1, 'c': 10, 'b': 2}

Like, it makes the argument name and argument into a dictionary. inspect is useful in many ways.

Recommended Posts

I want to memoize including Python keyword arguments
I want to debug with Python
I want to use jar from python
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
I want to do Dunnett's test in Python
I want to use MATLAB feval with python
I want to create a window in Python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to make a game with Python
I want to merge nested dicts in Python
I want to use Temporary Directory with Python2
I want to use ceres solver from python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to sell Mercari by scraping python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to write to a file with Python
I want to display the progress in Python!
I want to write in Python! (1) Code format check
Even beginners want to say "I fully understand Python"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to handle optimization with python and cplex
[Python] I want to merge Excel files anyway (pandas.merge)
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I want to randomly sample a file in Python
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
I want to inherit to the back with python dataclass
I want to work with a robot in python.
[Python3] I want to generate harassment names from Japanese!
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to solve Sudoku (Sudoku)
Keyword arguments for Python functions
I want to be able to analyze data with Python (Part 3)
I want to initialize if the value is empty (python)
I want to specify another version of Python with pyvenv
maya Python I want to fix the baked animation again.
I want to be able to analyze data with Python (Part 1)
I want to do something like sort uniq in Python
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
[Python] I want to use the -h option with argparse
Effective Python Memo Item 19 Give optional behavior to keyword arguments
I want to send a message from Python to LINE Bot