-
-
Save esehara/6781998 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
def class_overrider(cls): | |
def __init__(self, value): | |
self.value = value | |
cls.__init__ = __init__ | |
return cls | |
@class_overrider | |
class Hoge: | |
def __init__(self, value): | |
self.value = value * 3 | |
def compute(self): | |
return self.value * 2 | |
if __name__ == '__main__': | |
hoge = Hoge(3) | |
print(hoge.compute()) #=> 6 |
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
# -*- coding: utf-8 -*- | |
def instance_return(cls): | |
return cls() | |
@instance_return | |
class Hoge: | |
def compute(self): | |
return self.value * 2 | |
if __name__ == "__main__": | |
hoge = Hoge | |
hoge.value = 3 | |
print hoge.compute() | |
#=>6 |
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
# -*- coding: utf-8 -*- | |
class HogeBase: | |
def __init__(self, value): | |
self.value = value | |
class Hoge(HogeBase): | |
def __init__(self, value): | |
super(self.__class__, self).__init__(value) | |
def compute(self): | |
return self.value * 2 | |
if __name__ == '__main__': | |
hoge = Hoge(3) | |
print(hoge.compute()) #=> 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment