What is first class function in Python

前端 未结 8 1237
情话喂你
情话喂你 2020-12-08 10:28

I am still confused about what first-class functions are. If I understand correctly, first-class functions should use one function as an object. Is

相关标签:
8条回答
  • 2020-12-08 11:17

    "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.

    0 讨论(0)
  • 2020-12-08 11:18

    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

    0 讨论(0)
提交回复
热议问题