1801 26

[其他] Python3.9有什么新的变化 [推广有奖]

  • 2关注
  • 3粉丝

本科生

47%

还不是VIP/贵宾

-

威望
0
论坛币
22344 个
通用积分
67.9850
学术水平
776 点
热心指数
776 点
信用等级
780 点
经验
30593 点
帖子
89
精华
0
在线时间
33 小时
注册时间
2019-3-26
最后登录
2023-5-7

楼主
玩于股涨之上 发表于 2021-12-12 13:20:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
Python 3.9 使用于基于 PEG 的新解析器替代 LL(1)。新解析器的性能与旧解析器大致相当,但 PEG 在设
计新语言特性时的形式化比 LL(1) 更灵活。我们将在 Python 3.10 及之后版本中开始使用这种灵活性。
ast 模块会使用新解析器并会生成与旧解析器一致的 AST。 在 Python 3.10 中,旧解析器将被移除,依赖于它的所有功能也将被移除(主要是 parser 模块,它早已被弃
用)。只有在 Python 3.9 中,你可以使用命令行开关 (-X oldparser) 或环境变量 (PYTHONOLDPARSER=1)
切换回 LL(1) 解析器。

• __import__() 现在会引发 ImportError 而不是 ValueError,后者曾经会在相对导入超出其
最高层级包时发生。(由 Ngalim Siregar 在 bpo-37444 中贡献。)
• Python 现在会获取命令行中指定的脚本文件名 (例如: python3 script.py) 的绝对路径:
__main__ 模块的 __file__ 属性将是一个绝对路径,而不是相对路径。现在此路径在当前
目录通过 os.chdir() 被改变后仍将保持有效。作为附带效果,回溯信息也将在此情况下为
__main__ 模块帧显示绝对路径。(由 Victor Stinner 在 bpo-20443 中贡献。)
• 在 Python 开发模式以及调试编译版本中,现在会针对字符串编码和解码操作检查 encoding 和 errors
参数。例如: open(), str.encode() 和 bytes.decode()。
默认设置下,为保证性能,errors 参数只会在第一次发生编码/解码错误时被检查,并且对于空字符
串 encoding 参数有时会被忽略。(由 Victor Stinner 在 bpo-37388 中贡献。)
• "".replace("", s, n) 对于所有非零的 n 都将返回 s 而不是空字符串。现在此方法会与
"".replace("", s) 保持一致。对于 bytes 和 bytearray 对象也有类似的修改。(由 Serhiy
Storchaka 在 bpo-28029 中贡献。)
• 任何有效的表达式现在都可被用作 decorator。在之前版本中,相关语法则更为严格。请参阅 PEP
614 了解详情。(由 Brandt Bucher 在 bpo-39702 中贡献。)
• 改进了 typing 模块的帮助信息。现在将为所有特殊形式和特殊通用别名 (例如 Union 和 List)
显示文档字符串。使用 help() 时传入通用别名例如 List[int] 将显示对应实体类型 (这里对应
的是 list) 的帮助信息。(由 Serhiy Storchaka 在 bpo-40257 中贡献。)
• aclose() / asend() / athrow() 的并行运行现在已被禁止,且 ag_running 现在会反映异步
生成器的实际运行状态。(由 Yury Selivanov 在 bpo-30773 中贡献。)
• 调用 __iter__ 方法时发生的非预期错误不会再被 in 运算符以及 operator 的 contains(),
indexOf() 和 countOf() 中的 TypeError 所掩盖。(由 Serhiy Storchaka 在 bpo-40824 中贡献。)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python operator replace running Contain

Make some actual use of your life.
用你

沙发
玩于股涨之上 发表于 2021-12-12 13:24:38
nntplib
现在 NNTP 和 NNTP_SSL 当它们的构造器所给定的超时参数为零以防止创建非阻塞套接字时会引发
ValueError。
已有 1 人评分经验 论坛币 收起 理由
yunnandlg + 100 + 100 精彩帖子

总评分: 经验 + 100  论坛币 + 100   查看全部评分

藤椅
数据洞见 发表于 2021-12-18 10:42:57
Loading and initializing a module
Whenever the Python interpreter interacts with an import or equivalent statement,
it does three operations, which are described in the next sections.
Loading a module
The Python interpreter searches for the specified module on a sys.path (to be discussed
in the Accessing packages from any location section) and loads the source code. This has
been explained in the Learning how import works section.
Setting special variables
In this step, the Python interpreter defines a few special variables, such as __name__,
which basically defines the namespace that a Python module is running in. The __
name__ variable is one of the most important variables.

板凳
数据洞见 发表于 2021-12-18 10:43:09
Case A – module as the main program
If you are running your module as the main program, the __name__ variable will be set
to the __main__ value regardless of whatever the name of the Python file or module
is. For example, when calcmain1.py is executed, the interpreter will assign the
hardcoded __main__ string to the __name__ variable. If we run myrandom.py
or mycalculator.py as the main program, the __name__ variable will automatically
get the value of __main__.
Therefore, we added an if __name__ == '__main__' line to all main scripts to
check whether this is the main execution program.

报纸
数据洞见 发表于 2021-12-18 10:43:19
Case B – module is imported by another module
In this case, your module is not the main program, but it is imported by another module.
In our example, myrandom and mycalculator are imported in calcmain1.py. As
soon as the Python interpreter finds the myrandom.py and mycalculator.py files,
it will assign the myrandom and mycalculator names from the import statement to
the __name__ variable for each module. This assignment is done prior to executing the
code inside these modules. This is reflected in Table 2.1.
Some of the other noticeable special variables are as follows:
• __file__: This variable contains the path to the module that is currently being
imported.
• __doc__: This variable will output the docstring that is added in a class or
a method. As discussed in Chapter 1, Optimal Python Development Life Cycle,
a docstring is a comment line added right after the class or method definition.
• __package__: This is used to indicate whether the module is a package or not. Its
value can be a package name, an empty string, or none. • __dict__: This will return all attributes of a class instance as a dictionary.
• dir: This is actually a method that returns every associated method or attribute as
a list.
• Locals and globals: These are also used as methods that display the local and
global variables as dictionary entries.

地板
数据洞见 发表于 2021-12-18 10:43:30
Executing the code
After the special variables are set, the Python interpreter executes the code in the file line
by line. It is important to know that functions (and the code under the classes) are not
executed unless they are not called by other lines of code. Here is a quick analysis of the
three modules from the execution point of view when calcmain1.py is run:
• mycalculator.py: After setting the special variables, there is no code to be
executed in this module at the initialization time.
• myrandom.py: After setting the special variables and the import statement, there
is no further code to be executed in this module at initialization time.
• calcmain1.py: After setting the special variables and executing the import
statements, it executes the following if statement: if __name__ == "__
main__":. This will return true because we launched the calcmain1.py file.
Inside the if statement, the my_main () function will be called, which in fact
then calls methods from the myrandom.py and mycalculator.py modules.
We can add an if __name__ == "__main__" statement to any module regardless
of whether it is the main program or not. The advantage of using this approach is that
the module can be used both as a module or as a main program. There is also another
application of using this approach, which is to add unit tests within the module.

7
数据洞见 发表于 2021-12-18 10:43:39
Standard modules
Python comes with a library of over 200 standard modules. The exact number varies from
one distribution to the other. These modules can be imported into your program. The list
of these modules is very extensive but only a few commonly used modules are mentioned
here as an example of standard modules:
• math: This module provides mathematical functions for arithmetic operations.
• random: This module is helpful to generate pseudo-random numbers using
different types of distributions.
• statistics: This module offers statistics functions such as mean, median,
and variance. • base64: This module provides functions to encode and decode data.
• calendar: This module offers functions related to the calendar, which is helpful
for calendar-based computations.

8
数据洞见 发表于 2021-12-18 10:43:54
Writing reusable modules
For a module to be declared reusable, it has to have the following characteristics:
• Independent functionality
• General-purpose functionality
• Conventional coding style
• Well-defined documentation
If a module or package does not have these characteristics, it would be very hard, if not
impossible, to reuse it in other programs. We will discuss each characteristic one by one.
Independent functionality
The functions in a module should offer functionality independent of other modules and
independent of any local or global variables. The more independent the functions are, the
more reusable the module is. If it has to use other modules, it has to be minimal.

9
数据洞见 发表于 2021-12-18 10:44:05
Generalization functionality
An ideal reusable module should focus on solving a general problem rather than a very
specific problem. For example, we have a requirement of converting inches to centimeters.
We can easily write a function that converts inches into centimeters by applying
a conversion formula. What about writing a function that converts any value in the
imperial system to a value in the metric system? We can have one function for different
conversions that may handle inches to centimeters, feet to meters, or miles to kilometers,
or separate functions for each type of these conversions. What about the reverse functions
(centimeters to inches)? This may not be required now but may be required later on
or by someone who is reusing this module. This generalization will make the module
functionality not only comprehensive but also more reusable without extending it.

10
数据洞见 发表于 2021-12-18 10:44:21
To illustrate the generalization concept, we will revise the design of the myrandom
module to make it more general and thus more reusable. In the current design, we define
separate functions for one-digit and two-digit numbers. What if we need to generate
a three-digit random number or to generate a random number between 20 and 30? To
generalize the requirement, we introduce a new function, get_random, in the same
module, which takes user input for lower and upper limits of the random numbers. This
newly added function is a generalization of the two random functions we already defined.
With this new function in the module, the two existing functions can be removed, or they
can stay in the module for convenience of use. Note that the newly added function is also
offered by the random library out of the box; the reason for providing the function in our
module is purely for illustration of the generalized function (get_random in this case)
versus the specific functions (random_1d and random_2d in this case).

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-12 05:47