GSP
GSP

Reputation: 1076

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.

Upvotes: 2

Views: 389

Answers (4)

Minion91
Minion91

Reputation: 1929

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)

Upvotes: 1

Marwan Alsabbagh
Marwan Alsabbagh

Reputation: 26778

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)

Upvotes: 2

Pierre GM
Pierre GM

Reputation: 20339

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

Upvotes: 2

Nadir Sampaoli
Nadir Sampaoli

Reputation: 5555

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

Upvotes: 4

Related Questions