current position:Home>Python 3.10 official release
Python 3.10 official release
2022-01-29 11:21:45 【Liu Zhijun】
Python The official version was released a few days ago 3.10, Although you may not apply it to the production environment immediately , However, it is recommended to upgrade and experience the following if conditions permit , If you have no conditions, just read my article directly , I've listed a few features that developers may be interested in , See which feature you're most looking forward to .
1、 More friendly error tips
expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
38: 4, 39: 4, 45: 5, 46: 5, 47: 5, 48: 5, 49: 5, 54: 6,
some_other_code = foo()
Copy code
For example, if you accidentally miss a closing curly bracket in this code , When running code , In previous versions, syntax errors were directly prompted :
File "example.py", line 3
some_other_code = foo()
^
SyntaxError: invalid syntax
Copy code
Without checking the code carefully, you really can't see at a glance what syntax errors are . And in the python3.10 in , Tips become very friendly and specific , Just tell you "{" Not closing , In this way, the positioning error will be quickly .
File "example.py", line 1
expected = {9: 1, 18: 2, 19: 2, 27: 3, 28: 3, 29: 3, 36: 4, 37: 4,
^
SyntaxError: '{' was never closed
Copy code
Similarly , And if you forget to add parentheses in the derivation , If the previous sentence is not correct, it will directly prompt grammatical errors
>>> {x,y for x,y in zip('abcd', '1234')}
File "<stdin>", line 1
{x,y for x,y in zip('abcd', '1234')}
^
SyntaxError: invalid syntax
Copy code
And now I'll tell you , Did you forget to add parentheses .
>>> {x,y for x,y in zip('abcd', '1234')}
File "<stdin>", line 1
{x,y for x,y in zip('abcd', '1234')}
^
SyntaxError: did you forget parentheses around the comprehension target?
Copy code
Um. , This is humanized .
2、match ... case Finally here
match ... case Grammar is the function I'm looking forward to , It's not much of an advanced feature , Similar to... In other languages switch ... case grammar , In multi conditional judgment, it is better than if ... elif The code is simpler . It is hard to imagine , This grammar is only now added , Of course , In limine Python The father is unwilling to add this grammatical feature , Fortunately, this grammar finally returned , And changed its name .
I was thinking , Why can't you live with yourself , Unity is called switch ... case Is it not good? ? Maybe this is Python Fascinating place .
Take an example
This is the use of 3.10 Of match case grammar
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
Copy code
case _
Similar to... In other languages default , When other conditions are not met, execute this line .
With normal if ... else Grammar to write
def http_error(status):
if status == 400:
return "Bad request"
elif status == 404:
return "Not found"
elif status == 418:
return "I'm a teapot"
else:
return "Something's wrong with the internet"
Copy code
3、 Context manager that supports parentheses
In previous versions , Multiple context managers must be placed on one line or with escape characters “\” Line break
with open("xxx.py", mode="w") as f1, open("yyy.py", mode="w") as f2:
pass
# perhaps
with open("xxx.py", mode="w") as f1, \
open("yyy.py", mode="w") as f2:
pass
Copy code
stay 3.10 in , We can use parentheses to put multiple managers on multiple lines , This makes the code look cleaner .
with (
open("xxx.py", mode="w") as f1,
open("yyy.py", mode="w") as f2
):
pass
Copy code
4、 New type union operator
In the previous version , For function parameters, if you want to support multiple types , For example, it also supports int and float, Need to use Union:
from typing import Union
def foo(number: Union[ int, float]) -> Union[int, float]:
return number ** 2
Copy code
Now there is a new grammar sugar “|”, It's called the union operator , Can make the code more concise
def square(number: int | float) -> int | float:
return number ** 2
Copy code
The operator is in the function isinstance()
and issubclass()
Can also support
# python3.10
>>> isinstance(1, int | str)
True
# python3.7
>>> isinstance(1, (int,float))
True
Copy code
Last
When developers ask Python Whether there will be Python4.0 When ,Python My father said frankly not to Python 4.0 Hope . If it is released one day Python4.0, It won't go again 2.x Over to 3.0 Old road . meanwhile , We can't count on Python Of GIL Can remove , It's not that I haven't tried , But get rid of it GIL Then it's slower . If your project is very performance sensitive , Might as well try. pypy,python A branch of .
This article is published on the blog at the same time :foofish.net
Welcome to the official account
copyright notice
author[Liu Zhijun],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291121428540.html
The sidebar is recommended
- [Python introduction project] use Python to generate QR code
- Compile D + +, and use d to call C from python
- Quickly build Django blog based on function calculation
- Python collects and monitors system data -- psutil
- Finally, this Python import guide has been sorted out. Look!
- Quickly build Django blog based on function calculation
- Python interface test unittest usage details
- Implementation of top-level design pattern in Python
- You can easily get started with Excel. Python data analysis package pandas (VII): breakdown
- Python simulation random coin toss (non optimized version)
guess what you like
-
Python tiktok 5000+ V, and found that everyone love this video.
-
Using linear systems in python with scipy.linalg
-
Using linear systems in python with scipy.linalg
-
Together with Python to do a license plate automatic recognition system, fun and practical!
-
You can easily get started with Excel. Python data analysis package pandas (XI): segment matching
-
Advanced practical case: Javascript confusion of Python anti crawling
-
Using linear systems in python with scipy.linalg
-
Fast power modulus Python implementation of large numbers
-
Quickly build Django blog based on function calculation
-
This paper clarifies the chaotic switching operation and elegant derivation of Python
Random recommended
- You can easily get started with Excel pandas (I): filtering function
- You can easily get started with Excel. Python data analysis package pandas (II): advanced filtering (I)
- You can easily get started with Excel. Python data analysis package pandas (2): advanced filtering (2)
- You can easily get started with Excel. Python data analysis package pandas (3): making score bar
- Test Development: self study Dubbo + Python experience summary and sharing
- You can easily get started with Excel. Python data analysis package pandas (V): duplicate value processing
- How does Python correctly call jar package encryption to get the encrypted value?
- Python 3 interview question: give an array. If there is 0 in the array, add a 0 after 0, and the overall array length remains the same
- Python simple Snake game (single player mode)
- Using linear systems in python with scipy.linalg
- Python executes functions and even code through strings! Come and understand the operation of such a top!
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- [Python introduction project] use Python to generate QR code
- Vanessa basks in her photos and gets caught up in the golden python. There are highlights in the accompanying text. She can't forget Kobe after all
- [windows] Python installation pyteseract
- [introduction to Python project] create bar chart animation in Python
- Fundamentals of Python I
- Python series tutorials 116
- Python code reading (chapter 35): fully (deeply) expand nested lists
- Practical series 1 ️⃣ Wechat applet automatic testing practice (with Python source code)
- Python Basics: do you know how to use lists?
- Solution of no Python 3.9 installation was detected when uninstalling Python
- [Python homework] coupling network information dissemination
- [common links of Python & Python]
- Python application software development tool - tkinterdesigner v1.0 5.1 release!
- [Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger
- [Python development tool Tkinter designer]: Lecture 2: introduction to Tkinter designer's example project
- [Python development tool Tkinter designer]: Lecture 1: introduction to the basic functions of Tkinter Designer
- [introduction to Python tutorial] use Python 3 to teach you how to extract any HTML main content
- Python socket implements UDP server and client
- Python socket implements TCP server and client
- leetcode 1261. Find Elements in a Contaminated Binary Tree(python)
- [algorithm learning] 1486 Array XOR operation (Java / C / C + + / Python / go / trust)
- leetcode 1974. Minimum Time to Type Word Using Special Typewriter(python)
- The mobile phone uses Python to operate picture files
- [learning notes] Python exception handling try except...
- Two methods of using pandas to read poorly structured excel. You're welcome to take them away
- Python sum (): the summation method of Python
- Practical experience sharing: use pyo3 to build your Python module
- Using Python to realize multitasking process