current position:Home>Python code reading (Chapter 61): delaying function calls
Python code reading (Chapter 61): delaying function calls
2022-02-01 07:47:55 【FelixZ】
Python Code reading collection Introduction : Why not recommend Python Beginners directly look at the project source code
The code read in this article is implemented after a given delay time , Call the function of the specified function .
The code snippet read in this article comes from 30-seconds-of-python.
delay
from time import sleep
def delay(fn, ms, *args):
sleep(ms / 1000)
return fn(*args)
# EXAMPLES
delay(
lambda x: print(x),
1000,
'later'
) # prints 'later' after one second
Copy code
delay
Function receives a specified function fn
, One delay time ms
And specify the parameters of the function *args
, After a specified delay , Returns the specified function fn
Is the result of the call .
Function USES sleep()
Method to delay , Then the specified function is called. .delay
When the function is called , have access to lambda
Anonymous function of expression , You can also use general functions . The thing to notice is when fn
An exception occurs when a keyword parameter exists .
>>> from time import sleep
>>>
>>> def delay(fn, ms, *args):
... sleep(ms / 1000)
... return fn(*args)
...
>>> def f(pos_only, /, standard, *, kwd_only):
... print('pos_only:{}\nstandard:{}\nkwd_only:{}'.format(pos_only,standard,kwd_only))
...
>>> delay(f, 1000, 1, 2, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in delay
TypeError: f() takes 2 positional arguments but 3 were given
>>> delay(f, 1000, 1, 2, kwd_only = 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: delay() got an unexpected keyword argument 'kwd_only'
Copy code
copyright notice
author[FelixZ],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010747544565.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