Python callable type specification

Type specification of callable types (including lambda expressions)

pyBTrade.py


#Callable object:func
#Argument 1:int
#Argument 2:float
#Return value:str

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

See official documentation for details https://docs.python.org/ja/3/library/typing.html#typing.Callable

Me and this implementation example

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

Recommended Posts

Python callable type specification
Python numeric type
Python2 string type
Python # string type
Check Python # type identity
Python --Check type of values
Python immutable type int memo
Python data type summary memo
Blender 2.9, Python, hexadecimal color specification
Image data type conversion [Python]
Function argument type definition in python
[Translation] Python static type, amazing mypy!
Practice! !! Introduction to Python (Type Hints)
Python
Python for super beginners Python # dictionary type 1 for super beginners
[python] week1-3: Number type and operation
Dynamically load json type in python
Timezone specification when converting a string to datetime type in python
Type specified in python. Throw exceptions
Multi-line size specification reading with python
[Personal memo] Python sequence type / mapping type
Master the type with Python [Python 3.9 compatible]
Python for super beginners Python # dictionary type 2 for super beginners
[Introduction to Udemy Python3 + Application] 28. Collective type
[Python] Easy argument type check with dataclass
Use PostgreSQL data type (jsonb) from Python
Python learning basics ~ What is type conversion? ~
A Java programmer studied Python. (About type)
[Introduction to Udemy Python3 + Application] 21. Tuple type
Type annotations for Python2 in stub files!
python generated csv-like definition single element type
Receive date type (datetime) with ArgumentParser [python]
[python] [meta] Is the type of python a type?
[Introduction to Udemy Python3 + Application] 24. Dictionary type
Python> Format specification when printing `%`> "% s% s"% ('Hello','World',)
Python and NumPy numeric type inheritance relationship
Python basic course (4 numeric type / character string type)
Axis option specification summary of Python "numpy.sum (...)"
[Introduction to Udemy Python3 + Application] 16. List type