What’s New in Python 3.10 🚀

Takanori Suzuki

PyCon JP 2021 / 2021 Oct 15

Thanks to the Organizers 👏

Who am I / お前誰よ 👤

../_images/sokidan-square.jpg

久しぶりのリアル登壇 🤪

Python 3.10.0

Release Date: Oct. 4, 2021 🎉

Who knows 3.10 is released 🙋‍♂️

3.10出たの知ってる人 🙋‍♀️

Anyone installed 3.10 🙋‍♀️

3.10インストールした人 🙋‍♂️

Who can say 6 new features in 3.10 🙋‍♂️

3.10の新機能6つ言える人 🙋‍♀️

What’s New in Python 3.10 🆕

What’s New in Python 3.10 🆕

../_images/whatsnew310.png

Python Release Python 3.10.0

../_images/python3.10.0.png

Who are you? / お前誰だ? 🐍

https://user-images.githubusercontent.com/11718525/135937807-fd3e0fd2-a31a-47a4-90c6-b0bb1d0704d4.png

Parenthesized context managers

# 3.10
with (
    open('craftbeer.txt') as f1,
    open('beer-in-kanda.txt') as f2,
):
    ...
with open('craftbeer.txt') as f1, \
     open('beer-in-kanda.txt') as f2
    ...

Better error messages

Better error messages

beer_types = ['Pilsner', 'Ale', 'IPA', 'Hazy IPA'
print(beer_types)
$ python3.10 beer_styles.py
  File ".../beer_styles.py", line 1
    beer_styles = ['Pilsner', 'Ale', 'IPA', 'Hazy IPA'
                  ^
SyntaxError: '[' was never closed
$ python3.9 beer_styles.py
  File ".../beer_styles.py", line 2
    print(beer_styles)
    ^
SyntaxError: invalid syntax

Better error messages

# 3.10
>>> if beer_syle = 'IPA':
  File "<stdin>", line 1
    if beer_syle = 'IPA':
       ^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. \
  Maybe you meant '==' or ':=' instead of '='?
>>> if beer_syle = 'IPA':
  File "<stdin>", line 1
    if beer_syle = 'IPA':
                 ^
SyntaxError: invalid syntax

Better typing syntax

PEP 604: New Type Union Operator

  • Union[X, Y]X | Y

  • Optional[X]X | None

# 3.10
def drink_beer(number: int | float) -> str | None
    if am_i_full(number):
        return 'I'm full'
def drink_beer(number: Union[int, float]) -> Optional[str]
    if am_i_full(number):
        return 'I'm full'

PEP 613: TypeAlias

# 3.10
BeerStr: TypeAlias = 'Beer[str]'  # a type alias
LOG_PREFIX = 'LOG[DEBUG]'  # a module constant
BeerStr = 'Beer[str]'  # a type alias
LOG_PREFIX = 'LOG[DEBUG]'  # a module constant

Better typing syntax

  • Python 3.7 - 3.9

from __future__ import annotations

Advertise / 宣伝 📣

『Python 実践レシピ』 📕

  • 2022年1月 発売予定 / 技術評論社

  • 著者: 鈴木たかのり、筒井隆次、寺田学、杉田雅子、門脇諭、福田隼也

  • ページ数、金額: 調整中

  • 『Pythonライブラリ厳選レシピ』を 大加筆、大改訂!!!

大絶賛レビュー中 🔥

../_images/pylibbook2.png

Python 3.9対応

  • 残念ながら3.10の新機能の話はタイプヒントがちょこっと入ってるだけです

宣伝終わり

Structural Pattern Matching

Structural Pattern Matching

Syntax of Structural Pattern Matching

match subject:
    case <pattern_1>:
        <action_1>
    case <pattern_2>:
        <action_2>
    case <pattern_3>:
        <action_3>
    case _:
        <action_wildcard>

Simple pattern

match beer_style:
    case 'Pilsner':
        return "First drink"
    case 'IPA':
        return "I like it"
    case 'Hazy IPA':
        return "Cloudy and cloudy"
    case _:
        return "I like most beers"

For more information / 詳しくは…

Keynote Day 2: 明日のキーノートで

Brandt Bucher

https://1.bp.blogspot.com/-3eyCcV9cSEk/YRjlW5vfMHI/AAAAAAAADnM/qpPRy05gMEMJEka6BT55NmljWIsRk3fyACLcBGAsYHQ/s1203/profile.jpg

Reference / 参考資料

Thank you !! 🙏

Takanori Suzuki (@takanory)

slides.takanory.net

../_images/sokidan-square.jpg