current position:Home>The unknown side of Python functions
The unknown side of Python functions
2022-01-31 09:52:14 【somenzz】
Usually we define a function , Then the function is called. , The code related to the function begins to execute . But many people don't know , When we define a function , Some code starts to execute . Today, let's talk about the unknown side of functions .
Let's start with a piece of code :
def do_something(opt: print(' Parameters opt'), arg=print(' Parameters arg')) -> print(' The return value of the function '):
print("do something runing")
if __name__ == '__main__':
pass
Copy code
In the above code, we set a function , But it was not called , Does it output information ?
It will be , In the parameter, whether it is the type prompt part , Or the default assignment part , Or the type prompt of the return value , The statements inside will be executed , give the result as follows :
This practice is very rare for me , In fact print Open file instead of function , Connect to the database and other operations , Will also be executed .
But usually , Parameters are immutable types , If you pass in a variable type , Maybe every function call , The results will be changed , for example :
def do_something(opt: print(" Parameters opt"), arg=[]) -> print(" The return value of the function "):
print("do something runing")
print(f"{arg = }")
arg.append(0)
if __name__ == "__main__":
do_something(opt=1)
do_something(opt=1)
Copy code
The operation results are as follows :
You can find , Call the function twice do_something, Although not introduced arg Parameters ,arg The value of has sent a change . If you don't pay attention to this , There may be a bug happen . stay Pythcarm in , Will warn us arg It's a mutable object :
If you want to get the type prompt of the function 、 The default value is , It can be like this :
def do_something(opt: 1, arg=2) -> 3:
print("do something runing")
print(f"{arg = }")
arg.append(0)
if __name__ == "__main__":
print(f"{do_something.__annotations__ = }")
print(f"{do_something.__defaults__ = }")
#do_something.__annotations__ = {'opt': 1, 'return': 3}
#do_something.__defaults__ = (2,)
Copy code
Last
This article shares Python The unknown side of functions , If there is a harvest , Please thumb up 、 Looking at 、 Focus on supporting .
copyright notice
author[somenzz],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310952003968.html
The sidebar is recommended
- Django ORM details - fields, attributes, operations
- Python web crawler - crawling cloud music review (3)
- Stroke list in python (bottom)
- What cat is the most popular? Python crawls the whole network of cat pictures. Which one is your favorite
- [algorithm learning] LCP 06 Take coins (Java / C / C + + / Python / go / trust)
- Python shows the progress of downloading files from requests
- Solve the problem that Django celery beat prompts that the database is incorrectly configured and does not support multiple databases
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python quantitative data warehouse construction 3: data drop library code encapsulation
- The source code and implementation of Django CSRF Middleware
guess what you like
-
Python hashlib module
-
The cover of Python 3 web crawler development (Second Edition) has been determined!
-
The introduction method of executing Python source code or Python source code file (novice, please enter the old bird and turn left)
-
[Python basics] explain Python basic functions in detail, including teaching and learning
-
Python web crawler - crawling cloud music review (4)
-
The first step of scientific research: create Python virtual environment on Linux server
-
Writing nmap scanning tool in Python -- multithreaded version
-
leetcode 2057. Smallest Index With Equal Value(python)
-
Bamboolib: this will be one of the most useful Python libraries you've ever seen
-
Python crawler actual combat, requests module, python realizes capturing a video barrage
Random recommended
- [algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)
- Test platform series (71) Python timed task scheme
- Java AES / ECB / pkcs5padding encryption conversion Python 3
- Loguru: the ultimate Python log solution
- Blurring and anonymizing faces using OpenCV and python
- How fast Python sync and async execute
- Python interface automation test framework (basic) -- common data types list & set ()
- Python crawler actual combat, requests module, python realizes capturing video barrage comments of station B
- Python: several implementation methods of multi process
- Sword finger offer II 054 Sum of all values greater than or equal to nodes | 538 | 1038 (Java / C / C + + / Python / go / trust)
- How IOS developers learn python programming 3-operator 2
- How IOS developers learn python programming 2-operator 1
- [Python applet] 8 lines of code to realize file de duplication
- Python uses the pynvml tool to obtain the working status of GPU
- Data mining: Python actual combat multi factor analysis
- Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency
- Use Python VTK to batch read 2D slices and display 3D models
- Complete image cutting using Python version VTK
- Python interface automation test framework (basic) -- common data types Dict
- Django (make an epidemic data report)
- Python specific text extraction in actual combat challenges the first step of efficient office
- Daily python, Part 8 - if statement
- Django model class 1
- The same Python code draws many different cherry trees. Which one do you like?
- Python code reading (Chapter 54): Fibonacci sequence
- Django model class 2
- Python crawler Basics
- Mapping 3D model surface distances using Python VTK
- How to implement encrypted message signature and verification in Python -- HMAC
- leetcode 1945. Sum of Digits of String After Convert(python)
- leetcode 2062. Count Vowel Substrings of a String(python)
- Analysis of Matplotlib module of Python visualization
- Django permission management
- Python integrated programming -- visual hot search list and new epidemic situation map
- [Python data collection] scripy realizes picture download
- Python interface automation test framework (basic part) -- loop statement of process control for & while
- Daily python, Chapter 9, while loop
- Van * Python | save the crawled data with docx and PDF
- Five life saving Python tips
- Django frequency control