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 map
、filter
、reduce
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.
https://en.pythonmana.com/2022/02/202202011003161234.html
The sidebar is recommended
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
guess what you like
-
Python series tutorial 127 -- Reference vs copy
-
Control flow in Python: break and continue
-
Teach you how to extract tables in PDF with Python
-
leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(python)
-
leetcode 1338. Reduce Array Size to The Half(python)
-
Object oriented and exception handling in Python
-
How to configure load balancing for Django service
-
How to embed Python in go
-
Python Matplotlib drawing graphics
-
Python object-oriented programming 05: concluding summary of classes and objects
Random recommended
- Python from 0 to 1 (day 14) - Python conditional judgment 1
- Several very interesting modules in Python
- How IOS developers learn Python Programming 15 - object oriented programming 1
- Daily python, Chapter 20, exception handling
- Understand the basis of Python collaboration in a few minutes
- [centos7] how to install and use Python under Linux
- leetcode 1130. Minimum Cost Tree From Leaf Values(python)
- leetcode 1433. Check If a String Can Break Another String(python)
- Python Matplotlib drawing 3D graphics
- Talk about deep and shallow copying in Python
- Python crawler series - network requests
- Python thread 01 understanding thread
- Analysis of earthquake distribution in the past 10 years with Python~
- You need to master these before learning Python crawlers
- After the old friend (R & D post) was laid off, I wanted to join the snack bar. I collected some data in Python. It's more or less a intention
- Python uses redis
- Python crawler - ETF fund acquisition
- Detailed tutorial on Python operation Tencent object storage (COS)
- [Python] comparison of list, tuple, array and bidirectional queue methods
- Go Python 3 usage and pit Prevention Guide
- Python logging log error and exception exception callback method
- Learn Python quickly and take a shortcut~
- Python from 0 to 1 (day 15) - Python conditional judgment 2
- Python crawler actual combat, requests module, python to capture headlines and take beautiful pictures
- The whole activity collected 8 proxy IP sites to pave the way for the python proxy pool, and the 15th of 120 crawlers
- Why can't list be used as dictionary key value in Python
- Python from 0 to 1 (day 16) - Python conditional judgment 3
- What is the python programming language?
- Python crawler reverse webpack, a real estate management platform login password parameter encryption logic
- Python crawler reverse, a college entrance examination volunteer filling platform encrypts the parameter signsafe and decrypts the returned results
- Python simulated Login, selenium module, python identification graphic verification code to realize automatic login
- Python -- datetime (timedelta class)
- Python's five strange skills will bring you a sense of enrichment in mastering efficient programming skills
- [Python] comparison of dictionary dict, defaultdict and orderdict
- Test driven development using Django
- Face recognition practice: face recognition using Python opencv and deep learning
- leetcode 1610. Maximum Number of Visible Points(python)
- Python thread 03 thread synchronization
- Introduction and internal principles of Python's widely used concurrent processing Library Futures
- Python - progress bar artifact tqdm usage