How Transforms make use of TypeDispatch
Okay, here’s another one! I couldn’t have imagined that I will ever understand this part of V2, but now that I do, it just seems surreal! This is Python at a next level! And when you come to think of it, you can understand why it’s built this way.
But, lets discuss the thought process a little later.
First let’s understand encodes and decodes inside Transform!
So, from _TfmDict
class _TfmDict(dict):
def __setitem__(self,k,v):
if k=='_': k='encodes'
if k not in ('encodes','decodes') or not isinstance(v,Callable): return super().__setitem__(k,v)
if k not in self: super().__setitem__(k,TypeDispatch())
res = self[k]
res.add(v)
As long as something is not of type encodes or decodes the namespace of the cls would be created using dict as per normal behavior. Note, that __setitem__ is responsible for setting k:v inside dict, thus if you update that, you can get custom behavior!
So as long as something is not encodes or decodes, just use dict to set k:v.
BUT, when it is encodes or decodes then k:TypeDispatch()
And as we know - TypeDispatch is nothing but a cool dict of type:function mapping!
So theoretically speaking, the namespace of this special class which is a subclass of TfmMeta will look something like
{....all the usual stuff like __module__:__main__etc AND
encodes:
{
bool: some_func1,
int: some_func2,
Numbers.Integral: some_func3
},
decodes:
{
bool: some_reverse_func1,
int: some_reverse_func2,
Numbers.Integral: some_reverse_func3
},
And finally ! When you call encodes or decodes - it can be done so for different types, which will be called using __call__ inside TypeDispatch which then call the specific corresponding function to type!
It is all making sense now.
Please correct me if I have understood anything wrong 
