Python 模擬試験

Python実践 模擬試験 第4回 問14・5章 タイプヒント(基本的な型注釈)

get_origin と get_args

次のコードを実行したときの出力として、正しいものを1つ選べ。

from typing import get_origin, get_args
print(get_origin(list[int]), get_args(dict[str, int]))
  1. A

    <class 'list'> (<class 'str'>, <class 'int'>)

  2. B

    <class 'list'> (<class 'int'>,)

  3. C

    list[int] (str, int)

  4. D

    None None

正解と解説を見る

正解:A

typing.get_origin は元の型(list)、get_args は型引数を返す。実行時に型注釈を解析するライブラリ(pydantic など)が使う。

順実行する行実行後の変数
1`from typing import get_origin, get_args`—
2`print(get_origin(list[int]), get_args(dict[st…`—
結果実際の出力<class 'list'> (<class 'str'>, <class 'int'>)

学習のポイント:list[int] のような書き方は Python 3.9 以降。

この問題を演習で解く →

問題一覧へ

このサイトの問題は、各試験の公式に示された出題範囲に沿って作ったオリジナルの模擬問題です。本試験の問題ではありません。一般社団法人Pythonエンジニア育成推進協会の公式サービスではありません。