Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Higher order functions in Python

My apologies for what some may regard as a fundamental question. In the following simple code:

def greet(name):
    def say_hi():
        print('Preparing to greet...')
        print('Hi', name, '!')       
        print('Greeting given.')
    return say_hi

What is the sequence of events when 'greet' is called with a formal parameter and the interpreter encounters the 'say_hi' function. I see that a reference to it is returned (forming a closure I assume?), but is the inner function executed or simply 'read' and not called until the programmer writes code like the following:

f = greet('Caroline')
f()
like image 217
CaitlinG Avatar asked Dec 24 '15 19:12

CaitlinG


People also ask

What is higher-order function example?

Note: Functions such as filter(),map(),reduce(),some() etc, these all are example of Higher-Order Functions.

What is a higher-order function?

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself a procedure), returns a function as its result.

Is lambda a higher-order function in Python?

Lambda functions are frequently used with higher-order functions, which take one or more functions as arguments or return one or more functions. Python exposes higher-order functions as built-in functions or in the standard library. Examples include map() , filter() , functools.

What is a higher-order function in functional programming?

A higher-order function is a function that takes one or more functions as arguments and/or returns a function as its result. Three incredibly useful higher-order functions are map, filter, and reduce (fold). These functions allow large volumes of data to be operated on, filtered, and reduced to a single value.


1 Answers

Since every thing in python is about runtime (except compile time tasks like peephole optimizer and etc.), python doesn't call your function unless you call it.

You can see this behavior by using dis function from dis module, which returns the relative bytecode of your function :

>>> def greet(name):
...     def say_hi():
...         print('Preparing to greet...')
...         print('Hi', name, '!')       
...         print('Greeting given.')
...     return say_hi
... 
>>> import dis
>>> 
>>> dis.dis(greet)
  2           0 LOAD_CLOSURE             0 (name)
              3 BUILD_TUPLE              1
              6 LOAD_CONST               1 (<code object say_hi at 0x7fdacc12c8b0, file "<stdin>", line 2>)
              9 MAKE_CLOSURE             0
             12 STORE_FAST               1 (say_hi)

  6          15 LOAD_FAST                1 (say_hi)
             18 RETURN_VALUE  

As you can see in part 6 python just load the function as a code object in a CONST value.

like image 126
Mazdak Avatar answered Oct 24 '22 06:10

Mazdak