12.1. Callback

12.1.1. Rationale

  • EN: Callback Design

  • PL:

12.1.2. Implementation

>>> from http import HTTPStatus
>>> import requests
>>>
>>>
>>> def noop(*arg, **kwargs):
...     pass
>>>
>>>
>>> def http_request(url, on_success=noop, on_error=noop):
...     result = requests.get(url)
...     if result.status_code == HTTPStatus.OK:
...         on_success(result)
...     else:
...         on_error(result)
>>>
>>>
>>> def success(result):
...     print('Success')
>>>
>>>
>>> def error(result):
...     print('Error')
>>>
>>>
>>> # doctest: +SKIP
... http_request(
...     url='http://python.astrotech.io',
...     on_success=success,
...     on_error=error,
... )