Created
September 30, 2013 01:18
-
-
Save Gab-km/6758226 to your computer and use it in GitHub Desktop.
賢明なる Pythonista の諸兄に於かれましては、どちらの書き方をなさいますでしょうか。
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
# -*- coding: utf-8 -*- | |
def class_extender(cls): | |
def __init__(self, value): | |
self.value = value | |
cls.__init__ = __init__ | |
return cls | |
@class_extender | |
class Hoge: | |
def compute(self): | |
return self.value * 2 | |
if __name__ == '__main__': | |
hoge = Hoge(3) | |
print(hoge.compute()) #=> 6 |
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
# -*- 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