GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • Python Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — First Class Functions in Python - SyntaxDB - Python Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Print
      • User Input
      • Commenting
      • Read from Text File
      • Write to File
      • Import Module
    • Variables
      • Variable Declaration
      • Lists
      • List Comprehensions
      • Tuples
      • Dictionaries
      • Sets
    • Control Flow
      • If Statement
      • For Loop
      • While Loop
      • Ternary Operator
      • Try Catch Block
      • With Statement
      • Generators
    • Functions
      • Functions
      • First Class Functions
      • Lambda
      • Map
      • Filter
    • Object Oriented Programming
      • Class Declaration & Structure
      • Class and Instance Variables
      • Constructor
      • Instantiation
      • Methods
      • Inheritance
Categories
Language
Python
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 3.4.3

Categories

  • User Program Communication
    • Print
    • User Input
    • Commenting
    • Read from Text File
    • Write to File
    • Import Module
  • Variables
    • Variable Declaration
    • Lists
    • List Comprehensions
    • Tuples
    • Dictionaries
    • Sets
  • Control Flow
    • If Statement
    • For Loop
    • While Loop
    • Ternary Operator
    • Try Catch Block
    • With Statement
    • Generators
  • Functions
    • Functions
    • First Class Functions
    • Lambda
    • Map
    • Filter
  • Object Oriented Programming
    • Class Declaration & Structure
    • Class and Instance Variables
    • Constructor
    • Instantiation
    • Methods
    • Inheritance

Functions

First Class Functions in Python

First class functions are used for declaring functions within functions, as well as passing functions as arguments. Functions that contain the declared or passed functions are called high order functions. Functions within another function are called low order functions.


Syntax
def high_order_function(passed_params, lower_function):
    #call passed in function
    lower_function(passed_params)
    #declare another function within the higher order function
    def another_lower_function(passed_params):
        #code to execute for this function
        return some_value
    #lower order functions can be used as return statements
    return another_lower_function

Notes

Lower order functions have access to variables initialized in the high order function.

Lower order functions often use tuples and dictionaries as parameters.


Example
#first class function
def compare(operator, a, b):
    return operator(a, b)

#lower order functions
def min(a, b):
    return a if a < b else b

def max(a, b):
    return a if a > b else b

def equals(a, b):
    return true if a == b else false

#calling first class function
result = compare(max, 5, 10)

Add to Slack

< Functions   |   Lambda >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.