What is first class function in Python

前端 未结 8 1236
情话喂你
情话喂你 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 10:53

    First Class functions in Python:

    The “first-class” concept only has to do with functions in programming languages. First-class functions means that the language treats functions as values – that you can assign a function into a variable, pass it around etc. It’s seldom used when referring to a function, such as “a first-class function”. It’s much more common to say that “a language has/hasn’t first-class function support”.So "has first-class functions" is a property of a language.

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

    So, languages which support values with function types, and treat them the same as non-function values, can be said to have "first class functions".

    0 讨论(0)
  • 2020-12-08 10:53
    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.

    0 讨论(0)
  • 2020-12-08 10:57

    This is a good example to illustrate Python first-class functions in their classical form: a variable holding a lambda function:

    twice = lambda x: 2 * x
    d = twice(5)
    print(d)  # 10
    
    0 讨论(0)
  • 2020-12-08 10:59

    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.

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

    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.

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

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

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