Python deep nesting factory functions

六眼飞鱼酱① 提交于 2019-12-10 23:13:53

问题


Working through "Learning Python" came across factory function. This textbook example works:

def maker(N):
    def action(X):
        return X ** N
    return action


>>> maker(2)
<function action at 0x7f9087f008c0>
>>> o = maker(2)
>>> o(3)
8
>>> maker(2)
<function action at 0x7f9087f00230>
>>> maker(2)(3)
8

However when going deeper another level I have no idea how to call it:

>>> def superfunc(X):
...     def func(Y):
...             def subfunc(Z):
...                     return X + Y + Z
...     return func
... 
>>> superfunc()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: superfunc() takes exactly 1 argument (0 given)
>>> superfunc(1)
<function func at 0x7f9087f09500>
>>> superfunc(1)(2)
>>> superfunc(1)(2)(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> superfunc(1)(2)
>>>

Why doesn't superfunc(1)(2)(3) work while maker(2)(3) does?

While this kind of nesting certainly doesn't look like a good, usable code to me, Python still accepts it as valid, so I'm curious as to how this can be called.


回答1:


You get a TypeError because function func doesn't return anything (thus its return is NoneType). It should return subfunc:

>>> def superfunc(X):
...     def func(Y):
...             def subfunc(Z):
...                     return X + Y + Z
...             return subfunc
...     return func
... 



回答2:


A return is missing somewhere in your superfunc: you have a return for subfunc, but none for func.




回答3:


superfunc corrected, with a calling example

def superfunc(X):
    def func(Y):
        def subfunc(Z):
            return X + Y + Z
        return subfunc
    return func

print superfunc(1)(2)(3)



回答4:


You forgot the return of the second function. Here is the fixed function

def superfunct(x):
  def func(y):
    def subfunc(z):
      return x + y + z
    return subfunc
  return func

print superfunct(1)(2)(3)


来源:https://stackoverflow.com/questions/12687277/python-deep-nesting-factory-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!