You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -42,16 +42,18 @@ The ``register()`` attribute returns the undecorated function which enables deco
42
42
... print("Half of your number:", end="")
43
43
... print(arg /2)
44
44
45
-
The ``register()`` attribute only works inside a class statement, relying on ``SingleDispatch.__init_subclass__``
46
-
to create the actual dispatch table. This also means that (unlike functools.singledispatch) two methods
45
+
The ``register()`` method relys on ``SingleDispatch.__init_subclass__``
46
+
to create the actual dispatch table rather than adding the function directly.
47
+
This also means that (unlike functools.singledispatchmethod) two methods
47
48
with the same name cannot be registered as only the last one will be in the class dictionary.
48
49
49
-
Functions not defined in the class can be registered using the ``add_overload`` attribute.
50
+
Functions not defined in the class can be registered with a generic method using the ``add_overload`` method.
50
51
51
52
>>> defnothing(obj, arg, verbose=False):
52
53
... print('Nothing.')
53
54
>>> MyClass.fun.add_overload(type(None), nothing)
54
55
56
+
Using ``add_overload`` will affect all instances of ``MyClass`` as if it were part of the class declaration.
55
57
When called, the generic function dispatches on the type of the first argument
56
58
57
59
>>> a = MyClass()
@@ -72,9 +74,10 @@ Nothing.
72
74
>>> a.fun(1.23)
73
75
0.615
74
76
75
-
Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation. The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found.
77
+
Where there is no registered implementation for a specific type, its method resolution order is used to find a more generic implementation.
78
+
The original function decorated with ``@singledispatch`` is registered for the base ``object`` type, which means it is used if no better implementation is found.
76
79
77
-
To check which implementation will the generic function choose for a given type, use the ``dispatch()`` attribute
80
+
To check which implementation will the generic function choose for a given type, use the ``dispatch()`` method
78
81
79
82
>>> a.fun.dispatch(float)
80
83
<function MyClass.fun_num at 0x1035a2840>
@@ -84,55 +87,90 @@ To check which implementation will the generic function choose for a given type,
84
87
To access all registered implementations, use the read-only ``registry`` attribute
Subclasses can extend the type registry of the function on the base class with their own overrides.
96
100
The ``SingleDispatch`` mixin class ensures that each subclass has it's own independant copy of the dispatch registry
97
101
98
102
>>> classSubClass(MyClass):
99
103
... @MyClass.fun.register(str)
100
104
... deffun_str(self, arg, verbose=False):
101
-
... print('str')
105
+
... print('subclass')
102
106
...
103
107
>>> s = SubClass()
104
108
>>> s.fun('hello')
105
-
str
109
+
subclass
106
110
>>> b = MyClass()
107
111
>>> b.fun('hello')
108
112
hello
109
113
110
-
Method overrides do not need to provide the ``register`` decorator again to be used in the dispatch of ``fun``
114
+
Overriding the dispatch table
115
+
-----------------------------
116
+
There are two ways to override the dispatch function for a given type.
117
+
One way is to override a base-class method that is in the base class dispatch table.
118
+
Method overrides do not need to provide the ``register`` decorator again to be used in the dispatch of ``fun``, you can
119
+
simply override the specific dispatch function you want to modify.
111
120
112
-
>>> classSubClass2(MyClass):
121
+
>>> classMixin1(MyClass):
113
122
... deffun_int(self, arg, verbose=False):
114
123
... print('subclass int')
115
124
...
116
-
>>> s =SubClass2()
125
+
>>> s =Mixin1()
117
126
>>> s.fun(1)
118
127
subclass int
119
128
120
-
However, providing the register decorator with the same type will also work.
121
-
Decorating a method override with a different type (not a good idea) will register the different type and leave the base-class handler in place for the orginal type.
129
+
The other way is to register a method with the same type using the `register` method.
0 commit comments