Change language

First class functions in Python

First class function properties:

  • A function is an instance of type Object.
  • You can store a function in a variable.
  • You can pass a function as a parameter to another function.
  • You can return a function from a function.
  • You can store them in data structures such as hash tables, lists, ...

Examples illustrating first class functions in Python

1. Functions are objects: Python functions are first class objects. In the example below, we are assigning a function to a variable. This assignment does not call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.

# Python program to illustrate functions
# can be viewed as objects

def shout ( text):

return text. upper ()

 

print shout ( ’Hello’ )

 

yell = shout

 

print yell ( ’Hello’ )

Exit

 HELLO HELLO 

2. Functions can be passed as arguments to other functions: Since functions are objects, we can pass them as arguments to other functions. Functions that can take other functions as arguments are also called higher-order functions. In the example below, we have created a greet function that takes a function as an argument.

# Python program to illustrate functions
# can be passed as arguments to other functions

def shout (text):

  return text.upper ()

  

def whisper (text):

return text.lower ()

 

def greet (func):

  # store the function in a variable

  greeting = func ( " Hi, I am created by a function passed as an argument. " )

  print greeting 

  
greet (shout)
greet (whisper)

Exit

 HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT. hi, i am created by a function passed as an argument. 

3. Functions can return another function: Since functions are objects, we can return a function from another function. In the example below, the create_adder function returns the adder function.

 25 

This article courtesy of Mayank Agrawal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method

# Python program to illustrate functions
# Functions can return another function

 

def create_adder (x):

def adder (y):

return x + y

 

return adder

 

add_15 = create_adder ( 15 )

 

print add_15 ( 10 )