Xen_mar
Xen_mar

Reputation: 9722

Decorating Functions

I'm a python beginner and I struggle with a somewhat easy concept of functional programming. I simply can not see why the nested function (see example below) would even be called. This is quite hard to explain.

func3 = func1(func2)

Here I call func1 with the positional argument func2. All fair and well. func1 returns a nested function. But when I run the code the function nest1has been executed even though I've only called func1 (I dont see why this should execute the nested function). func1 should not do more than returning a function without printing anything. Can someone explain this to me?

def func2():

  def nest2():
      print('Nest2')

  nest2()
  return nest2


def func1(func):

   def nest1():
       print('Nest1')
       func()

   return nest1


func3 = func1(func2)

func3()

If I call a simple function with a nested function the inner function is not executed. See below:

def new_func():
   def inner():
       print(1)
   return inner

new_func()

Upvotes: 0

Views: 69

Answers (4)

Danil Speransky
Danil Speransky

Reputation: 30463

Body of func2 is:

def nest2():
    print('Nest2')

nest2()

return nest2

And so body of func1(func2) is:

print('Nest1')

def nest2():
    print('Nest2')

nest2()

return nest2

The last piece we get by replacing func with body of func2 in body of nest1.

Upvotes: 0

Chris
Chris

Reputation: 22963

But when I run the code the function nest1 has been executed even though I've only called func1 (I dont see why this should execute the nested function). Func 1 should not do more than returning a function without printing anything. Can someone explain this to me?

When you do this:

func3 = func1(func2)

You pass the function func2 into the function func1. Inside of func1, you define a function nest1 that calls the function passed in. This function is then returned and becomes the value of func3. The only thing func1 does is create the nest1 function, and return it. When func3 is called, you actually called the function defined inside of func1, nest1.

If I call a simple function with a nested function the inner function is not executed.

The reason your second example did not work as expected, is because calling new_func does not call inner, it only creates inner and returns it. This is the same case as above. Calling func1 did not call nest1, it simply created nest1 and returned it. You had to explitly call nest1 (func3()). Likewise, you need to call the function returned from new_func explicitly:

new_func()()

Upvotes: 1

user955340
user955340

Reputation:

func1() returns the method nest1 (without executing it), so your call func3 = func1(func2) returns nest1 and assigns it to func3.

The next line you are executing whatever is assigned to func3 - which is the method nest1. In other words, the line func3() is executing the function nest1, and causing the text to print.

Upvotes: 2

Ajax1234
Ajax1234

Reputation: 71461

In the final example, you need to call the function that is being returned with an extra ():

def new_func():
   def inner():
      print(1)
   return inner

new_func()()

Output:

1

Upvotes: 0

Related Questions