Skip to content

Commit

Permalink
add notes about functions in typing
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyguitar committed Aug 13, 2018
1 parent cc459dd commit 91919b7
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions docs/notes/python-typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,45 @@ Basic types
var_map_dict: Mapping[str, str] = {"foo": "Foo"}
var_mutable_dict: MutableMapping[str, str] = {"bar": "Bar"}
Functions
----------
.. code-block:: python
from typing import Generator, Callable
# function
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
# callback
def fun(cb: Callable[[int, int], int]) -> int:
return cb(55, 66)
# lambda
f: Callable[[int], int] = lambda x: x * 2
Generator
----------
.. code-block:: python
from typing import Generator
# Generator[YieldType, SendType, ReturnType]
def fib(n: int) -> Generator[int, None, None]:
a: int = 0
b: int = 1
while n > 0:
yield a
b, a = a + b, b
n -= 1
g: Generator = fib(10)
i: Iterator[int] = (x for x in range(3))
Avoid ``None`` access
----------------------
Expand Down Expand Up @@ -213,34 +252,6 @@ Multiple return values
a, b = foo(1, 2) # ok
c, d = bar(3, "bar") # ok
Generator function
-------------------
.. code-block:: python
from typing import Generator
# Generator[YieldType, SendType, ReturnType]
def fib(n: int) -> Generator[int, None, None]:
a: int = 0
b: int = 1
while n > 0:
yield a
b, a = a + b, b
n -= 1
# or
from typing import Iterable
def fib(n: int) -> Iterable[int]:
a: int = 0
b: int = 1
while n > 0:
yield a
b, a = a + b, b
n -= 1
Union[Any, None] == Optional[Any]
----------------------------------
Expand Down

0 comments on commit 91919b7

Please sign in to comment.