Python教程:Python3.8瞭解差不多了,Python3.9新特性瞭解一下

本期Python教程將解釋 Python 3.9 中的新特性,而不是 3.8。官網目前也只有 3.8 的下載包,3.9 需要自己編譯 Cpython。

語言上的變化

1、使用 Python 進行相對導包的時候,__import__ 出現異常時類型由原來的 ValueError 變成了 ImportError。(由 Ngalim Siregar 在 bpo-37444 中貢獻)

"""Resolve a relative module name to an absolute one."""
bits = package.rsplit('.', level - 1)
if len(bits) < level:
- raise ValueError('attempted relative import beyond top-level package')
+ raise ImportError('attempted relative import beyond top-level package')
base = bits[0]
return '{}.{}'.format(base, name) if name else base

-:github 中的刪除

+:github 中的增加

補充知識:

__import__() 函數一般用於動態加載類和函數。

代碼示例

r = __import__('requests_html', globals(), locals(), ['HTMLSession'], 0) 
session = r.HTMLSession()
print(session.get("http://www.baidu.com"))
#globals() 函數會以字典類型返回當前位置的全部全局變量。
#locals() 函數會以字典類型返回當前位置的全部局部變量。

ImportError 觸發異常原因:在涉及到相對導入時,package 所對應的文件夾必須正確的被 python 解釋器視作 package ,而不是普通文件夾。否則由於不被視作 package,無法利用 package 之間的嵌套關係實現 Python 中包的相對導入。

2、Python 現在獲取在命令行上指定的腳本文件名的絕對路徑(例如:python>

__main__模塊的絕對路徑。(由 Victor Stinner 在 bpo-20443 中貢獻。)

通過命令行執行文件的時候

import sys
print(f"{__file__=}")
print(f"{sys.argv=}")
print(f"{sys.path[0]=}")

運行

$ ./python3/>

結果

__file__='/Users/chenxiangan/cpython/script.py'
sys.argv=['/Users/chenxiangan/cpython/script.py']
sys.path[0]='/Users/chenxiangan/cpython'

但是對於下面這段代碼,這段代碼請在 Python3.8 下運行

script.js
import sys
import os
modname = 'relpath'
filename = modname + '.py'
sys.path.insert(0, os.curdir)
with open(filename, "w") as fp:
print("import sys", file=fp)
print("mod = sys.modules[__name__]", file=fp)
print("print(f'{__file__=}')", file=fp)
print("print(f'{mod.__file__=}')", file=fp)
print("print(f'{mod.__cached__=}')", file=fp)
__import__(modname)
os.unlink(filename)

這個代碼意思是動態生產下面的代碼

import sys
mod = sys.modules[__name__]
print(f'{__file__=}')
print(f'{mod.__file__=}')
print(f'{mod.__cached__=}')

然後執行完上面的代碼,通過 os.unlink 刪除。

輸出下面的結果

__file__='./relpath.py'
mod.__file__='./relpath.py'
mod.__cached__='./__pycache__/relpath.cpython-38.pyc'

可以看到還是相對路徑,這問題是 Cpython 的 Moudles/getpath.c 的一個 bug 修改內容如下

* absolutize() should help us out below
*/
else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) &&
- _Py_isabs(execpath))
+ (wchar_t) execpath[0] == SEP)
{
size_t len;
wchar_t *path = Py_DecodeLocale(execpath, &len);

3、在開發模式和調試模式中,使用 encoding 和 decoding 操作的時候加入 encoding 和 errors 兩個關鍵字參數,errors 是聲明在編碼或者解碼的時候出現錯誤要如何處理。

例如 str.encode() 和 bytes.decode()。它們的語法結構分別是

str.encode(encoding="utf-8", errors="strict")
bytes.decode(encoding="utf-8", errors="strict")¶
Python教程:Python3.8瞭解差不多了,Python3.9新特性瞭解一下

改進的模塊

classmethod

類方法現在可以裝飾其他描述符了,比如property()。

class C:
@classmethod
def f(cls):
pass
@classmethod
@property
def age(cls):
print("haha")
if __name__ == "__main__":
c=C()
c.age
print("over")

輸出

haha
over


asyncio

loop.shutdown_default_executor()

調度默認執行程序的關閉,並等待它連接ThreadPoolExecutor中的所有線程。調用此方法後,如果在使用默認執行程序時調用executor()中的loop.run,則會引發RuntimeError。

注意,使用asyncio.run()時不需要調用這個函數。

loop.shutdown_default_executor()

threading

在子解釋器中,生成守護進程線程現在會引發異常。子解釋器中從不支持守護進程線程。在此之前,如果守護進程線程仍然在運行,則子解釋器終止過程會出現 Python 致命錯誤。(來自 Victor Stinner 提出的 bpo-37266.)方法release,在3.9版本中更改,添加了n參數來同時釋放多個等待的線程。

loop.set_default_executor(executor)

將executor設置為executor()中的run使用的默認執行程序。executor應該是ThreadPoolExecutor的一個實例。

從3.8版開始就不推薦:不推薦使用不是ThreadPoolExecutor實例的執行程序,Python 3.9中會觸發異常。要求executor必須是concurrent.futures.ThreadPoolExecutor的實例。

all_tasks

從3.7版開始就被棄用了,3.9版中將會刪除:不要把它作為任務方法調用。使用asyncio.all_tasks()函數取代。同樣的current_task也是用函數asyncio.current_task()取代。

pprint

pprint 現在可以打印漂亮的 types.SimpleNamespace。

補充說明:

SimpleNamespace 繼承自 object,其作用用來代替 class X: pass 語句

代碼:

import types
import pprint
o = types.SimpleNamespace( the=0,
quick=1,
brown=2,
fox=3,
jumped=4,
over=5,
a=6,
lazy=7,
dog=8)
pprint.pprint(o)

改版前輸出

namespace(a=6, brown=2, dog=8, fox=3, jumped=4, lazy=7, over=5, quick=1, the=0)


改版後輸出:

namespace(the=0,
quick=1,
brown=2,
fox=3,
jumped=4,
over=5,
a=6,
lazy=7,
dog=8,
c=3)

importlib

提高與 import 語句的一致性 importlib.util.resolve_name() 的異常類型也該為了 ImportError 以前是 ValueError。

不再推薦使用的模塊用法

  • parse 模塊已被棄用,並將在未來的 Python 版本中刪除。對於大多數用例,用戶可以使用 ast 模塊利用抽象語法樹 (AST) 生成和編譯階段。
  • random 模塊之前接受任何的 hashable 類型作為種子值,不幸的是,其中一些類型不能保證具有確定性的散列值。Python3.9 中種子值將只接受 None, int, float, str, bytes, and bytearray 類型。


移除的模塊用法

  • math.factorial(x)

從3.9版本開始不贊成,帶有整數值的浮點數(比如5.0)。下面代碼示例

>>> import math
>>> math.factorial(3)
6

>>> math.factorial(3.0)
<stdin>:1: DeprecationWarning: Using factorial() with floats is deprecated
6
/<stdin>
  • collection.abc 裡面的抽象基類[https://docs.python.org/3.9/library/collections.abc.html#collections-abstract-base-classes],將不在常規的 collection 模塊中公開,這有助於在具體類和抽象基類之間創建更清晰的區別。
  • 刪除了從 Python 3.2 開始就棄用的 sys.getcheckinterval() 和 sys.setcheckinterval() 函數。它使用 sys.getswitchinterval() 和 sys.setswitchinterval() 來代替。主要作用分別是返回和設置解釋器的 “線程切換時間間隔”。
  • 刪除了從 Python 3.8 開始不再推薦使用的 threading.Thread 的 isAlive() 方法,使用 is_alive() 代替。
  • 移除 ElementTree 中在 Python3.2 就已經廢棄的方法,getchildren() 和 getiterator(),以 list() 和 iter() 代替。同時刪除 xml.etree.cElementTree 方法。
  • 刪除 3.4 中不支持的舊 plistlib 模塊的實現。使用 load(), loads(), dump(), and dumps() 方法。此外,use_builtin_types 參數已刪除,始終使用標準字節對象代替。

補充說明:

這個模塊提供了一個接口來讀寫蘋果使用的屬性列表文件,主要是在 macOS 和 iOS 上。該模塊支持二進制和 XML plist 文件。

  • 修正了當 AssertionError 被隱藏時斷言語句的錯誤行為。加入 LOAD_ASSERTION_ERROR 操作碼。

後記

需要注意的是這個文檔目前只是個草稿格式,隨著 Python3.9 的正式發佈,一些特性可能還會添加或刪除。


分享到:


相關文章: