8.17. Functional Pattern Callback¶
Callback Design Pattern
8.17.1. Example¶
>>> from urllib.request import urlopen
>>>
>>>
>>> def fetch(url: str,
... on_success = lambda response: ...,
... on_error = lambda error: ...,
... ) -> None:
... try:
... result = urlopen(url).read().decode('utf-8')
... except Exception as error:
... on_error(error)
... else:
... on_success(result)
>>> fetch(
... url = 'https://python3.info',
... on_success = lambda resp: print(resp),
... on_error = lambda err: print(err),
... )
>>> def ok(response: str):
... print(response)
>>>
>>> def err(error: Exception):
... print(error)
>>>
>>>
>>> fetch(url='https://python3.info')
>>> fetch(url='https://python3.info', on_success=ok)
>>> fetch(url='https://python3.info', on_error=err)
>>> fetch(url='https://python3.info', on_success=ok, on_error=err)
>>> fetch(url='https://python3.info/not-existing', on_error=err)