current position:Home>[Python] functions, higher-order functions, anonymous functions and function attributes

[Python] functions, higher-order functions, anonymous functions and function attributes

2022-02-01 10:03:18 Hazelnut

「 This is my participation 11 The fourth of the yuegengwen challenge 5 God , Check out the activity details :2021 One last more challenge

First class objects and first class functions

stay Python in , Integers 、 Strings and dictionaries are first-class objects . Functions are also first-class objects . People often will “ Treat functions as first-class objects ” Referred to as “ The wait function ”.

Programming language theorists “ First class object ” Defined as a procedural entity that meets the following conditions :

  • Create at run time
  • Can be assigned to an element in a variable or data structure
  • Can be passed to a function as a parameter
  • Can be the return result of a function

Higher order function

Accept a function as an argument , Or the function returned as a result is a higher-order function . Such as map and sorted .

In the functional programming paradigm , The most well-known higher-order functions are mapfilterreduce and apply .

stay Python 3 in ,map and filter Return to generator ( An iterator ). List derivation or generator expression has map and filter The function of two functions , And it's easier to read . Take a chestnut :

list(map(fact, range(6)))		# [1, 1, 2, 6, 24, 120]  Factorial 
[fact(n) for n in range(6)]		# [1, 1, 2, 6, 24, 120]

list(map(factorial, filter(lambda n: n % 2, range(6)))) 	# [1, 6, 120]
[factorial(n) for n in range(6) if n % 2] 					# [1, 6, 120]
 Copy code 

stay Python 2 in ,reduce It's a built-in function , But in Python 3 Put in functools In the module .

apply Function in Python 2.3 Marked obsolete in , stay Python 3 Removed from , Because I don't need it anymore . If you want to call a function with non quantitative parameters , You can write fn(*args, **keywords) , No more writing apply(fn, args, kwargs) .

Anonymous functions

lambda The key word in Python Create anonymous function within expression .lambda Syntax is just grammar sugar : And def The sentence is the same ,lambda Expressions create function objects .

Properties of functions

Use dir The function can view the properties of the function object :dir(function_name)

An instance of a class has no properties that a function has :

name type explain
__annotations__ dict Comments on parameters and return values
__call__ method-wrapper Realization () Operator ; To call the object protocol
__closure__ tuple A function closure , Binding of free variables ( Usually None
__code__ code Function metadata and function definition body compiled into bytecode
__defaults__ tuple Default value of formal parameter
__get__ method-wrapper Implement the read-only descriptor protocol
__globals__ dict Global variables in the module where the function is located
__kwdefaults__ dict Only the default values of keyword form parameters
__name__ str The name of the function
__qualname__ str Qualified name of the function , Such as Random.choice

copyright notice
author[Hazelnut],Please bring the original link to reprint, thank you.

Random recommended