Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class M:
- def __init__(self, val=None, exc=None):
- self.val = val
- self.exc = exc
- def __getattr__(self, item):
- if self.exc:
- return M(exc=self.exc)
- if hasattr(self.val, item):
- return M(getattr(self.val, item), self.exc)
- raise Exception(f'missed attr {item}')
- def __call__(self, *args, **kwargs):
- try:
- return M(exc=self.exc) if self.exc\
- else M(val=self.val(*args, **kwargs))
- except Exception as err:
- return M(exc=err)
- def unwrap(self):
- if self.exc:
- raise self.exc
- return self.val
- class TheA:
- def a(self):
- return TheB()
- class TheB:
- def b(self, raise_exception):
- if raise_exception:
- raise Exception('Something went wrong')
- return TheC()
- class TheC:
- def c(self):
- return 42
- a = M(TheA()).a
- x = a().b(raise_exception=False).c().unwrap()
- print(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement