Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the new syntax of Python3.8

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the new grammar of Python3.8". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the new grammars of Python3.8"?

I. New Grammar

The most interesting part of Python 3.8is the change in syntax, which helps to improve efficiency and reduce code workload.

1. Walrus operator (: =)

Does this ": =" look a bit like the face of a walrus? This is a new Python syntax that can directly assign values to variables when making conditional judgments.

In the past, we needed to assign a variable first, and then judge the condition.

M = re.match (p1, line) if m: return m.group (1) else: M = re.match (p2, line) if m: return m.group (2) else: M = re.match (p3, line).

After using the walrus operator, we can directly assign a value to the variable:

If m: = re.match (p1, line): return m.group (1) elif m: = re.match (p2, line): return m.group (2) elif m: = re.match (p3, line):

There is another example used in loops where in the past you had to assign a value before looping on a variable:

Ent = obj.next_entry () while ent:... # process ent ent = obj.next_entry ()

You can now assign values while looping:

While ent: = obj.next_entry ():... # process ent

two。 F-string is supported in code debugging

F-string (or "format string") was added in Python version 3.6, and although this feature is very convenient, developers find that f-string is not helpful for debugging. Therefore, Eric V. Smith adds some syntax structures to f-string so that it can be used for debugging.

In the past, f-string could be used as follows:

Print (fancifoo = {foo} bar= {bar}')

In Python 3.8, you only need to use the following code (more concise):

Print (f'{foo=} {bar=}')

In both cases, the output is:

> foo=42 > bar= 'answer... > print (f' {foo=} {bar=}') foo=42 bar=answer.

In addition, you can specify the format of the output by adding the "! s" and "! f" commands after the assignment symbol, for example:

> import datetime > now= datetime.datetime.now () > print (f'{now=} {nowdays}') now=datetime.datetime (2019, 7, 16, 16, 58, 0, 680222) now=2019-07-16 16 purse 58 purl 00.680222

> import math > print (f'{math.pirates, fuzz. 2f}') math.pi=3.14

As shown in the code, the "! s" character is added after the equal sign of the second "now" variable, causing the now to change from output of type "datetime" to output of type string. In the output of "pi", due to the addition of "! FRV. 2F", the output of pi is changed to two decimal places.

The format in curly braces also affects the format of the printed result, for example:

> a = 37 > > print (f'{a =}, {a =}') a = 37, a = 37

The spacing before and after the equal sign in curly braces is different, and the spacing in the print result is also different.

3. Variable input that does not require Keyword

This syntax allows the function to specify that only Value can be entered when defining input variables, instead of adding Keyword before Value.

For example, when using the pow () function:

> pow (2,3) 8 > pow (xylene 2, yellow3). TypeError: pow () takes no keyword arguments

The first method is legal, but the second is illegal.

To ensure the first pure Python function approach, developers can use "/" when defining functions to specify which variables must be input in the first format. For example:

Def pow (x, y, z=None, /): r = x if z is not None: r% = z return r

An extra "/" character is added after all variables are defined, indicating that all Python variable inputs must be done as pow (x, y, z).

Of course, you can also insert "/" between variables, where the variables before the forward slash follow the pure Python input method, while those after the forward slash follow the defined method. For example, define the following function:

Def fun (a, b, /, c, d, *, e, f):.

Some of the following expressions are legal, but others are illegal:

Fun (1, 2, 3, 4, eBay 5, fudge 6) # legal fun (1, 2, 3, 4, 4, 5, 6) # legal fun (A, B, C, 4, 4, 5, 6) # illegal

II. Other characteristics

In addition to some grammatical improvements, there are some other changes in Python 3.8.

1. Removable "_ _ pycache__"

The _ _ pycache__ directory is created by the Python3 interpreter to save the. pyc file. These files hold the bytecode (byte code) after the. py file compiled by the interpreter. Previous versions of Python only created a .pyc file for each .py file, but the new version will change.

To support multiple versions of Python, including some that are not CPython (such as PyPy), the library file now creates a corresponding .pyc file for each Python version in the form of "name.interp-version.pyc". For example, the first time a foo.py file is used, a corresponding pyc file is created with the path "_ _ pycache__/foo.cpython-37.pyc", and this pyc file defines the Python version used.

two。 Other improvements

Python 3. 8 will add a faster invocation method for C language extensions, which originally belonged to CPython. In Python3.8, this feature is experimental, and the final completed version appears in Python3.9.

At the same time, the initialization configuration processing in the compiler has also been cleaned, so that Python can be better embedded in other programs without relying on environment variables or adding other components that cause conflicts in existing Python systems.

In addition, a large number of Python built-in modules have been improved and adjusted, such as "os.path", "shutil", "math", "ssl" and so on.

Thank you for your reading, the above is the content of "what is the new grammar of Python3.8". After the study of this article, I believe you have a deeper understanding of what the new grammar of Python3.8 has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report