Sample code for Chapter 1 of Fluent Python 2e by Luciano Ramalho (O'Reilly, 2020)
Use Python's standard doctest module to check stand-alone doctest file:
$ python3 -m doctest frenchdeck.doctest -v
And to check doctests embedded in a module:
$ python3 -m doctest vector2d.py -v
Install pytest and the nbval plugin:
$ pip install pytest nbval
Run:
$ pytest --nbval
__getitem__import collections
Card = collections.namedtuple('Card', ['rank', 'suit'])
beer_card = Card(7, 'diamonds')
# >>> beer_card
# Card(rank=7, suit='diamonds')| Category | Method names |
|---|---|
| String/bytes representation | __repr__, __str__, __format__, __bytes__, __fspath__ |
| Conversion to number | __abs__, __bool__, __complex__, __int__, __float__, __hash__, __index__ |
| Emulating collections | __len__, __getitem__, __setitem__, __delitem__, __contains__ |
| Iteration | __iter__, __aiter__, __next__, __anext__, __reversed__ |
| Callable or coroutine execution | __call__, __await__ |
| Context management | __enter__, __aenter__, __exit__, __aexit__ |
| Instance creation and destruction | __new__, __init__, __del__ |
| Attribute management | __getattr__, __getattribute__, __setattr__, __delattr__, __dir__ |
| Attribute descriptors | __get__, __set__, __delete__, __set_name__ |
| Class services | __prepare__, __init_subclass__, __instancecheck__, __subclasscheck__ |
| Category | Method names and related operators |
|---|---|
| Unary numeric operators | __neg__ -, __pos__ +, __abs__ abs() |
| Rich comparison operators | __lt__ <, __le__ <=, __eq__ ==, __ne__ !=, __gt__ >, __ge__ >= |
| Arithmetic operators | __add__ +, __sub__ -, __mul__ *, __truediv__ /, __floordiv__//, __mod__ %, __divmod__ divmod() , __pow__ ** or pow(), __round__ round(), __matmul__ @ |
| Reversed arithmetic operators | __radd__, __rsub__, __rmul__, __rtruediv__, __rfloordiv__, __rmod__, __rdivmod__, __rpow__, __rmatmul__ |
| Augmented assignment arithmetic operators | __iadd__, __isub__, __imul__, __itruediv__, __ifloordiv__, __imod__, __ipow__, __imatmul__ |
| Bitwise operators | __invert__ ~, __lshift__ <<, __rshift__ >>, __and__ &, __or__ | , __xor__ ^ |
| Reversed bitwise operators | __rlshift__, __rrshift__, __rand__, __rxor__, __ror__ |
| Augmented assignment bitwise operators | __ilshift__, __irshift__, __iand__, __ixor__, __ior__ |