Skip to content

Instantly share code, notes, and snippets.

@RemyPorter
Created April 4, 2019 21:16
Show Gist options
  • Save RemyPorter/7f746874340d264470afeb3109f4b4c6 to your computer and use it in GitHub Desktop.
Save RemyPorter/7f746874340d264470afeb3109f4b4c6 to your computer and use it in GitHub Desktop.
Stupid Decorator Tricks
class Foo(object):
def __init__(self):
self.value = 5
def wrap(param):
def _wrap(clss):
class _Wrap(clss):
def __init__(self):
super(_Wrap, self).__init__()
self.other_value = param
return _Wrap
return _wrap
@wrap(10)
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
b = Bar() # crashes on line 17 as it recursively calls `Bar.__init__`
print(b.value, b.other_value)
class Foo(object):
def __init__(self):
self.value = 5
def wrap(param):
def _wrap(clss):
class _Wrap(clss):
def __init__(self):
super(_Wrap, self).__init__()
self.other_value = param
return _Wrap
return _wrap
@wrap(10)
class Bar(Foo):
def __init__(self):
super().__init__()
b = Bar()
print(b.value, b.other_value) # prints `5 10`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment