What is first class function in Python

旧城冷巷雨未停 提交于 2019-11-30 01:53:49

A first-class function is not a particular kind of function. All functions in Python are first-class functions. To say that functions are first-class in a certain programming language means that they can be passed around and manipulated similarly to how you would pass around and manipulate other kinds of objects (like integers or strings). You can assign a function to a variable, pass it as an argument to another function, etc. The distinction is not that individual functions can be first class or not, but that entire languages may treat functions as first-class objects, or may not.

"First-Class Functions" (FCF) are functions which are treated as so called "First-Class Citizens" (FCC). FCC's in a programming language are objects (using the term "objects" very freely here) which:

  • Can be used as parameters
  • Can be used as a return value
  • Can be assigned to variables
  • Can be stored in data structures such as hash tables, lists, ...

Actually, very roughly and simply put, FCF's are variables of the type 'function' (or variables which point to a function). You can do with them everything you can do with a 'normal' variable.

Knowing this, both this_is_another_example(myarg) and this_is_example(myarg1) are First-Class Functions, since all functions are First-Class in certain programming languages.

Nelson Sequiera

No. You are talking about higher-order functions -- refer.

First class functions: If a function can be assigned to a variable or passed as object/variable to other function, that function is called as first class function.

Python, JavaScript and C(pointers) support first class functions.

A simple example (in python):

def square(x): return x * x
def cube(x): return x * x * x

def print_result(x, func):
    """recieve a function and execute it and return result"""
    return func(x)

do_square = square     # assigning square function to a variable
do_cube = cube         # assigning cube function to a variable

res = print_result(5, do_square)   # passing function to another function
print res
res = print_result(5, do_cube)
print res

This program is just for illustration.

def pie(r):
    def circleArea(d):
        return r * (d ** 2)
    return circleArea

c = pie(3.14)
print c(2)

Above is an example for first class function in python.

Every function in python is first class, because they can be passed around like any other object.

First Class functions in Python

First class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. A programming language is said to support first-class functions if it treats functions as first-class objects. Python supports the concept of First Class functions.

Properties of first class functions:
A function is an instance of the Object type.
- You can store the function in a variable. - You can pass the function as a parameter to another function. - You can return the 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 function to a variable. This assignment doesn’t 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 treated as objects
    def shout(text):  
        return text.upper()      
    print shout('Hello') 
    yell = shout 
    print yell('Hello')

Output:
HELLO
HELLO

  1. Functions can be passed as arguments to other functions: Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, we have created a function greet which 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):
        #storing the function in a variable
        greeting = func("Hi, I am created by a function passed as an argument.")
        print greeting     
    greet(shout)
    greet(whisper)

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

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


    #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)

Output:
25

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!