LoginSignup
0
1

More than 3 years have passed since last update.

Python 呼び出し可能型の型指定

Last updated at Posted at 2021-01-04

呼び出し可能型(lambda式含む)の型指定

pyBTrade.py
# 呼び出し可能オブジェクト:func
# 引数1:int
# 引数2:float
# 返り値:str

def test(func: Callable[[int, float], str]) -> None:

詳しくは公式ドキュメントを参照
https://docs.python.org/ja/3/library/typing.html#typing.Callable

自分とこの実装例

pyBTrade.py
    async def order_limit(
        self, 
        price: Decimal, 
        size: Decimal, 
        func: Callable[['TradeSim', COrderItem], bool] = None
    ) -> COrderItem:
        order: COrderItem = self.book_keeper_fx.order_limit(price, size)
        while True:
            try:
                await asyncio.sleep(0)
                if order.is_finished:
                    break
                if self.is_finished:
                    break
                if callable(func):
                    if func(self, order):
                        raise asyncio.CancelledError
            except asyncio.CancelledError:
                await self.send_order_cancel(order)
                break
        return order
0
1
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
0
1