Created
April 4, 2019 21:16
-
-
Save RemyPorter/7f746874340d264470afeb3109f4b4c6 to your computer and use it in GitHub Desktop.
Stupid Decorator Tricks
This file contains 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
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) |
This file contains 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
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