What is memoization and how can I use it in Python?

前端 未结 13 1147
情歌与酒
情歌与酒 2020-11-21 17:25

I just started Python and I\'ve got no idea what memoization is and how to use it. Also, may I have a simplified example?

13条回答
  •  鱼传尺愫
    2020-11-21 18:10

    The other answers cover what it is quite well. I'm not repeating that. Just some points that might be useful to you.

    Usually, memoisation is an operation you can apply on any function that computes something (expensive) and returns a value. Because of this, it's often implemented as a decorator. The implementation is straightforward and it would be something like this

    memoised_function = memoise(actual_function)
    

    or expressed as a decorator

    @memoise
    def actual_function(arg1, arg2):
       #body
    

提交回复
热议问题