Better illustration of Closures?

后端 未结 5 630
终归单人心
终归单人心 2020-12-30 06:41

I am learning Python using Dive Into Python 3 book. I like it, but I don\'t understand the example used to introduce Closures in Section 6.5.

I mean, I see how it wo

5条回答
  •  再見小時候
    2020-12-30 07:19

    here's an closure usage, of get configures:

    def confmaker():
       cf=ini_conf()
       def clo(*args):
          return cf.get(*args)
       return clo
    
    cfget=confmaker()
    
    cfget(...)
    

    here ini_conf is called only once. In my understanding, closures avoid global variables(like cf), and make usage simple.

提交回复
热议问题