|
378 | 378 | " self.funcs,self.bases = _TypeDict(),L(bases).filter(is_not(None))\n", |
379 | 379 | " for o in L(funcs): self.add(o)\n", |
380 | 380 | " self.inst = None\n", |
| 381 | + " self.owner = None\n", |
381 | 382 | "\n", |
382 | 383 | " def add(self, f):\n", |
383 | 384 | " \"Add type `t` and function `f`\"\n", |
|
406 | 407 | " f = self[tuple(ts)]\n", |
407 | 408 | " if not f: return args[0]\n", |
408 | 409 | " if self.inst is not None: f = MethodType(f, self.inst)\n", |
| 410 | + " elif self.owner is not None: f = MethodType(f, self.owner)\n", |
409 | 411 | " return f(*args, **kwargs)\n", |
410 | 412 | "\n", |
411 | 413 | " def __get__(self, inst, owner):\n", |
412 | 414 | " self.inst = inst\n", |
| 415 | + " self.owner = owner\n", |
413 | 416 | " return self\n", |
414 | 417 | "\n", |
415 | 418 | " def __getitem__(self, k):\n", |
|
550 | 553 | { |
551 | 554 | "data": { |
552 | 555 | "text/markdown": [ |
553 | | - "<h4 id=\"TypeDispatch.add\" class=\"doc_header\"><code>TypeDispatch.add</code><a href=\"__main__.py#L10\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
| 556 | + "<h4 id=\"TypeDispatch.add\" class=\"doc_header\"><code>TypeDispatch.add</code><a href=\"__main__.py#L11\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
554 | 557 | "\n", |
555 | 558 | "> <code>TypeDispatch.add</code>(**`f`**)\n", |
556 | 559 | "\n", |
|
873 | 876 | { |
874 | 877 | "data": { |
875 | 878 | "text/markdown": [ |
876 | | - "<h4 id=\"TypeDispatch.__call__\" class=\"doc_header\"><code>TypeDispatch.__call__</code><a href=\"__main__.py#L32\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
| 879 | + "<h4 id=\"TypeDispatch.__call__\" class=\"doc_header\"><code>TypeDispatch.__call__</code><a href=\"__main__.py#L33\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
877 | 880 | "\n", |
878 | 881 | "> <code>TypeDispatch.__call__</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n", |
879 | 882 | "\n", |
|
961 | 964 | { |
962 | 965 | "data": { |
963 | 966 | "text/markdown": [ |
964 | | - "<h4 id=\"TypeDispatch.returns\" class=\"doc_header\"><code>TypeDispatch.returns</code><a href=\"__main__.py#L20\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
| 967 | + "<h4 id=\"TypeDispatch.returns\" class=\"doc_header\"><code>TypeDispatch.returns</code><a href=\"__main__.py#L21\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n", |
965 | 968 | "\n", |
966 | 969 | "> <code>TypeDispatch.returns</code>(**`x`**)\n", |
967 | 970 | "\n", |
|
1080 | 1083 | "test_eq(a2.f(()), (1,))" |
1081 | 1084 | ] |
1082 | 1085 | }, |
| 1086 | + { |
| 1087 | + "cell_type": "markdown", |
| 1088 | + "metadata": {}, |
| 1089 | + "source": [ |
| 1090 | + "#### Using TypeDispatch With Class Methods\n", |
| 1091 | + "\n", |
| 1092 | + "You can use `TypeDispatch` when defining class methods too:" |
| 1093 | + ] |
| 1094 | + }, |
| 1095 | + { |
| 1096 | + "cell_type": "code", |
| 1097 | + "execution_count": null, |
| 1098 | + "metadata": {}, |
| 1099 | + "outputs": [], |
| 1100 | + "source": [ |
| 1101 | + "def m_nin(cls, x:(str,numbers.Integral)): return str(x)+'1'\n", |
| 1102 | + "def m_bll(cls, x:bool): cls.foo='a'\n", |
| 1103 | + "def m_num(cls, x:numbers.Number): return x*2\n", |
| 1104 | + "\n", |
| 1105 | + "t = TypeDispatch([m_nin,m_num,m_bll])\n", |
| 1106 | + "class A: f = t # set class attribute `f` equal to a TypeDispatch\n", |
| 1107 | + "\n", |
| 1108 | + "test_eq(A.f(1), '11') #dispatch to m_nin\n", |
| 1109 | + "test_eq(A.f(1.), 2.) #dispatch to m_num\n", |
| 1110 | + "test_is(A.f.owner, A)\n", |
| 1111 | + "\n", |
| 1112 | + "A.f(False) # this triggers t.m_bll to run, which sets A.foo to 'a'\n", |
| 1113 | + "test_eq(A.foo, 'a')" |
| 1114 | + ] |
| 1115 | + }, |
1083 | 1116 | { |
1084 | 1117 | "cell_type": "markdown", |
1085 | 1118 | "metadata": {}, |
|
1396 | 1429 | "Converted 04_transform.ipynb.\n", |
1397 | 1430 | "Converted 05_logargs.ipynb.\n", |
1398 | 1431 | "Converted 06_meta.ipynb.\n", |
| 1432 | + "Converted 07_script.ipynb.\n", |
1399 | 1433 | "Converted index.ipynb.\n" |
1400 | 1434 | ] |
1401 | 1435 | } |
|
0 commit comments