LoginSignup
1
0

More than 3 years have passed since last update.

Python でデフォルトの引数値のある関数に型ヒントをつける

Last updated at Posted at 2019-10-21

◯ 問題

関数にデフォルトの引数値をつけることができます。これにどうやれば型をつけられるのか分からずハマっていました。

def combiner(*vals: bytes, maxlen: Optional[int] = None) -> List[bytes]:
    ...

◯ 対応

Protocol を継承させたクラスに __call__ をつけます。

from typing import Protocol

class Combiner(Protocol):
    def __call__(self, *vals: bytes, maxlen: Optional[int] = None) -> List[bytes]:
        ...

◯ 補足

Protocol は、本来は静的にダックタイピングをしたいときに使うものです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0