Apa yang Baru di Python 3.9 — Dokumentasi Python 3.15.2
Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » Apa yang Baru di Python » Apa yang Baru di Python 3.9 | Tema | Apa yang Baru di Python 3.9 ¶ Editor : Łukasz Langa This article explains the new features in Python 3.9, compared to 3.8. Python 3.9 was released on October 5, 2020. For full details, see the changelog . Lihat juga PEP 596 - Jadwal Rilis Python 3.9 Ringkasan -- Sorotan Rilis ¶ Fitur sintaks baru: PEP 584 , union operators added to dict ; PEP 585 , type hinting generics in standard collections; PEP 614 , relaxed grammar restrictions on decorators.
Fitur bawaan baru: PEP 616 , metode string untuk menghapus prefiks dan sufiks. Fitur baru di pustaka standar: PEP 593 , fungsi fleksibel dan penjelasan variabel; os.pidfd_open() added that allows process management without races and signals. Peningkatan Interpreter: PEP 573 , fast access to module state from methods of C extension types; PEP 617 , CPython sekarang menggunakan parser baru berdasarkan PEG; sejumlah bawaan Python (range, tuple, set, frozenset, list, dict) sekarang dipercepat menggunakan PEP 590 vectorcall; pengumpulan sampah tidak memblokir objek yang dibangkitkan; a number of Python modules ( _abc , audioop , _bz2 , _codecs , _contextvars , _crypt , _functools , _json , _locale , math , operator , resource , time , _weakref ) now use multiphase initialization as defined by PEP 489; a number of standard library modules ( audioop , ast , grp , _hashlib , pwd , _posixsubprocess , random , select , struct , termios , zlib ) are now using the stable ABI defined by PEP 384.
Modul pustaka baru: PEP 615 , Basis Data Zona Waktu IANA sekarang ada di pustaka standar di modul zoneinfo module; implementasi semacam topologi grafik sekarang tersedia di modul baru graphlib . Perubahan proses rilis: PEP 602 , CPython mengadopsi siklus rilis tahunan. Anda harus memeriksa DeprecationWarning di kode Anda ¶ When Python 2.7 was still supported, a lot of functionality in Python 3 was kept for backward compatibility with Python 2.7.
With the end of Python 2 support, these backward compatibility layers have been removed, or will be removed soon. Most of them emitted a DeprecationWarning warning for several years. For example, using collections.Mapping instead of collections.abc.Mapping emits a DeprecationWarning since Python 3.3, released in 2012. Test your application with the -W default command-line option to see DeprecationWarning and PendingDeprecationWarning , or even with -W error to treat them as errors.
Warnings Filter can be used to ignore warnings from third-party code. Python 3.9 is the last version providing those Python 2 backward compatibility layers, to give more time to Python projects maintainers to organize the removal of the Python 2 support and add support for Python 3.9. Aliases to Abstract Base Classes in the collections module, like collections.Mapping alias to collections.abc.Mapping , are kept for one last release for backward compatibility.
They will be removed from Python 3.10. More generally, try to run your tests in the Python Development Mode which helps to prepare your code to make it compatible with the next Python version. Note: a number of pre-existing deprecations were removed in this version of Python as well. Consult the Removed section. Fitur-fitur Baru ¶ Dictionary Merge & Update Operators ¶ Merge ( | ) and update ( |= ) operators have been added to the built-in dict class.
Those complement the existing dict.update and {**d1, **d2} methods of merging dictionaries. Contoh: >>> x = { "key1" : "value1 from x" , "key2" : "value2 from x" } >>> y = { "key2" : "value2 from y" , "key3" : "value3 from y" } >>> x | y {'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'} >>> y | x {'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'} Lihat PEP 584 untuk penjelasan lengkap. (Kontribusi oleh Brandt Bucher di bpo-36144 .) Metode String Baru untuk Menghapus Prefiks dan Sufiks ¶ str.removeprefix(prefix) and str.removesuffix(suffix) have been added to easily remove an unneeded prefix or a suffix from a string.
Corresponding bytes , bytearray , and collections.UserString methods have also been added. See PEP 616 for a full description. (Contributed by Dennis Sweeney in bpo-39939 .) Type Hinting Generics in Standard Collections ¶ In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict ) from typing . Some other types in the standard library are also now generic, for example queue.Queue .
Contoh: def greet_all ( names : list [ str ]) -> None : for name in names : print ( "Hello" , name ) See PEP 585 for more details. (Contributed by Guido van Rossum, Ethan Smith, and Batuhan Taşkaya in bpo-39481 .) Pengurai Baru ¶ Python 3.9 menggunakan pengurai baru, berdasarkan PEG dari pada LL(1) . Kinerja parser baru kira-kira sebanding dengan parser lama, tetapi formalisme PEG lebih fleksibel daripada LL(1) saat merancang fitur bahasa baru.
Kami akan mulai menggunakan fleksibilitas ini di Python 3.10 dan yang lebih baru. The ast module uses the new parser and produces the same AST as the old parser. In Python 3.10, the old parser will be deleted and so will all functionality that depends on it (primarily the parser module, which has long been deprecated). In Python 3.9 only , you can switch back to the LL(1) parser using a command line switch ( -X oldparser ) or an environment variable ( PYTHONOLDPARSER=1 ).
See PEP 617 for more details. (Contributed by Guido van Rossum, Pablo Galindo and Lysandros Nikolaou in bpo-40334 .) Perubahan Bahasa Lainnya ¶ __import__() now raises ImportError instead of ValueError , which used to occur when a relative import went past its top-level package. (Contributed by Ngalim Siregar in bpo-37444 .) Python now gets the absolute path of the script filename specified on the command line (ex: python3 script.py ): the __file__ attribute of the __main__ module became an absolute path, rather than a relative path.
These paths now remain valid after the current directory is changed by os.chdir() . As a side effect, the traceback also displays the absolute path for __main__ module frames in this case. (Contributed by Victor Stinner in bpo-20443 .) In the Python Development Mode and in debug build , the encoding and errors arguments are now checked for string encoding and decoding operations. Examples: open() , str.encode() and bytes.decode() .
By default, for best performance, the errors argument is only checked at the first encoding/decoding error and the encoding argument is sometimes ignored for empty strings. (Contributed by Victor Stinner in bpo-37388 .) "".replace("", s, n) now returns s instead of an empty string for all non-zero n . It is now consistent with "".replace("", s) . There are similar changes for bytes and bytearray objects. (Contributed by Serhiy Storchaka in bpo-28029 .) Any valid expression can now be used as a decorator .
Previously, the grammar was much more restrictive. See PEP 614 for details. (Contributed by Brandt Bucher in bpo-39702 .) Improved help for the typing module. Docstrings are now shown for all special forms and special generic aliases (like Union and List ). Using help() with generic alias like List[int] will show the help for the correspondent concrete type ( list in this case). (Contributed by Serhiy Storchaka in bpo-40257 .) Parallel running of aclose() / asend() / athrow() is now prohibited, and ag_running now reflects the actual running status of the async generator. (Contributed by Yury Selivanov in bpo-30773 .) Unexpected errors in calling the __iter__ method are no longer masked by TypeError in the in operator and functions contains() , indexOf() and countOf() of the operator module. (Contributed by Serhiy Storchaka in bpo-40824 .) Unparenthesized lambda expressions can no longer be the expression part in an if clause in compreh
The rest of this page is on the live site.