Skip to content

Commit 2863213

Browse files
committed
fixes #85
1 parent 64c1ca0 commit 2863213

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

fastcore/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ def store_attr(names=None, self=None, but=None, **attrs):
9898
"Store params named in comma-separated `names` from calling context into attrs in `self`"
9999
fr = sys._getframe(1)
100100
args = fr.f_code.co_varnames[:fr.f_code.co_argcount]
101-
if self is None: self = fr.f_locals[args[0]]
101+
if self: args = ('self', *args)
102+
else: self = fr.f_locals[args[0]]
102103
if not hasattr(self, '__stored_args__'): self.__stored_args__ = {}
103104
if attrs: return _store_attr(self, **attrs)
104-
105105
ns = re.split(', *', names) if names else args[1:]
106106
_store_attr(self, **{n:fr.f_locals[n] for n in ns if n not in L(but)})
107107

nbs/02_utils.ipynb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
{
295295
"data": {
296296
"text/plain": [
297-
"<__main__._t at 0x7f79887ee6d0>"
297+
"<__main__._t at 0x7fd7b4badb90>"
298298
]
299299
},
300300
"execution_count": null,
@@ -507,10 +507,10 @@
507507
" \"Store params named in comma-separated `names` from calling context into attrs in `self`\"\n",
508508
" fr = sys._getframe(1)\n",
509509
" 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",
511512
" if not hasattr(self, '__stored_args__'): self.__stored_args__ = {}\n",
512513
" if attrs: return _store_attr(self, **attrs)\n",
513-
" \n",
514514
" ns = re.split(', *', names) if names else args[1:]\n",
515515
" _store_attr(self, **{n:fr.f_locals[n] for n in ns if n not in L(but)})"
516516
]
@@ -738,6 +738,28 @@
738738
"assert t.a==1"
739739
]
740740
},
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+
},
741763
{
742764
"cell_type": "code",
743765
"execution_count": null,

0 commit comments

Comments
 (0)