简体   繁体   中英

Python deep nesting factory functions

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.

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
... 

就返回某处遗失在superfunc :你有returnsubfunc的,但没有func

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM