How to access a function inside a function?

后端 未结 5 1856
清酒与你
清酒与你 2020-11-27 07:13

I am wondering how I can access a function inside another function. I saw code like this:

>>> def make_adder(x):
      def adder(y):
        return          


        
5条回答
  •  无人及你
    2020-11-27 07:51

    I'm not sure this helps, but you can use the __call__ boilerplate to simulate the behavior you're looking for. For example:

    class MakeAdder:
        def __init__(self, x):
            self.x = x
    
        def __call__(self, y):
            return self.x + y
    
    
    
    a = MakeAdder(5)
    a(10)
    15
    
    

提交回复
热议问题