|
294 | 294 | { |
295 | 295 | "data": { |
296 | 296 | "text/plain": [ |
297 | | - "<__main__._t at 0x7f79887ee6d0>" |
| 297 | + "<__main__._t at 0x7fd7b4badb90>" |
298 | 298 | ] |
299 | 299 | }, |
300 | 300 | "execution_count": null, |
|
507 | 507 | " \"Store params named in comma-separated `names` from calling context into attrs in `self`\"\n", |
508 | 508 | " fr = sys._getframe(1)\n", |
509 | 509 | " args = fr.f_code.co_varnames[:fr.f_code.co_argcount]\n", |
510 | | - " if self is None: self = fr.f_locals[args[0]]\n", |
| 510 | + " if self: args = ('self', *args)\n", |
| 511 | + " else: self = fr.f_locals[args[0]]\n", |
511 | 512 | " if not hasattr(self, '__stored_args__'): self.__stored_args__ = {}\n", |
512 | 513 | " if attrs: return _store_attr(self, **attrs)\n", |
513 | | - " \n", |
514 | 514 | " ns = re.split(', *', names) if names else args[1:]\n", |
515 | 515 | " _store_attr(self, **{n:fr.f_locals[n] for n in ns if n not in L(but)})" |
516 | 516 | ] |
|
738 | 738 | "assert t.a==1" |
739 | 739 | ] |
740 | 740 | }, |
| 741 | + { |
| 742 | + "cell_type": "markdown", |
| 743 | + "metadata": {}, |
| 744 | + "source": [ |
| 745 | + "You can also use store_attr inside functions." |
| 746 | + ] |
| 747 | + }, |
| 748 | + { |
| 749 | + "cell_type": "code", |
| 750 | + "execution_count": null, |
| 751 | + "metadata": {}, |
| 752 | + "outputs": [], |
| 753 | + "source": [ |
| 754 | + "def create_T(a, b):\n", |
| 755 | + " t = SimpleNamespace()\n", |
| 756 | + " store_attr(self=t)\n", |
| 757 | + " return t\n", |
| 758 | + "\n", |
| 759 | + "t = create_T(a=1, b=2)\n", |
| 760 | + "assert t.a==1 and t.b==2" |
| 761 | + ] |
| 762 | + }, |
741 | 763 | { |
742 | 764 | "cell_type": "code", |
743 | 765 | "execution_count": null, |
|
0 commit comments