diff options
Diffstat (limited to 'documentation.py')
-rw-r--r-- | documentation.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/documentation.py b/documentation.py index 53b42cd..78872f7 100644 --- a/documentation.py +++ b/documentation.py @@ -90,7 +90,7 @@ Definitions ------------------------------------ Technically speaking, any Python object which can be called with one argument -can be used as a decorator. However, this definition is somewhat too large +can be used as a decorator. However, this definition is somewhat too large to be really useful. It is more convenient to split the generic class of decorators in two subclasses: @@ -659,7 +659,7 @@ decorated with `dispatch_on` is turned into a generic function and it is the one which is called if there are no more specialized implementations. Usually such default function should raise a NotImplementedError, forcing peope to register some implementation. -The registration can be done as a decorator: +The registration can be done with a decorator: $$writefloat @@ -675,17 +675,24 @@ I could give a down-to-earth example of situations in which it is desiderable to dispatch on more than one argument (for instance once I implemented a database-access library where the first dispatching argument was the the database driver and the second the database record), but here I prefer -to follow the old tradition and show the time-honored +to follow the tradition and show the time-honored Rock-Paper-Scissor example: $$Rock $$Paper $$Scissor -I have added an ordinal to the Rock-Paper-Scissor classes to -simplify the implementation of the generic function. There are -9 combinations, however combinations with the same ordinal -correspond to parity. Moreover by exchanging. +I have added an ordinal to the Rock-Paper-Scissor classes to simplify +the implementation. The idea is to define a generic function `win(a, +b)` of two arguments corresponding to the moves of the first and +second player respectively. The moves are instances of the classes +Rock, Paper and Scissors; Paper wins over Rock, Scissor wins over +Paper and Rock wins over Scissor. The function with return +1 for a +win, -1 for a loss and 0 for parity. There are 9 combinations, however +combinations with the same ordinal (i.e. the same class) return 0; +moreover by exchanging the order of the arguments the sign of the +result changes, so it is enough to specify only 3 direct +implementations: $$win $$winRockPaper |