Last active
September 6, 2020 06:48
-
-
Save bhavsarpratik/2f78299030617b414ebcf7e44af95bb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, inspect | |
# Taken and modified from https://github.com/fastai/fastcore | |
class Base: | |
def _store_attr(self, **attrs): | |
for n,v in attrs.items(): | |
setattr(self, n, v) | |
self.__stored_args__[n] = v | |
def store_attr(self, names=None, but=[], **attrs): | |
"Store params named in comma-separated `names` from calling context into attrs in `self`" | |
fr = inspect.currentframe().f_back | |
args,varargs,keyw,locs = inspect.getargvalues(fr) | |
if self is None: self = locs[args[0]] | |
if not hasattr(self, '__stored_args__'): self.__stored_args__ = {} | |
if attrs: return self._store_attr(**attrs) | |
ns = re.split(', *', names) if names else args[1:] | |
self._store_attr(**{n:fr.f_locals[n] for n in ns if n not in but}) | |
class T(Base): | |
def __init__(self, a,b,c): | |
super().__init__() | |
self.store_attr() | |
class T2(T): | |
def __init__(self, d, **kwargs): | |
super().__init__(**kwargs) | |
self.store_attr() | |
t2 = T2(a=1,b=2,c=3,d=4) | |
print(t2.a) | |
print(t2.b) | |
print(t2.c) | |
print(t2.d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment