Last active
December 6, 2016 11:33
-
-
Save yuu-ito/d9d2058edbf7dbec8d2895cecfe8f39f to your computer and use it in GitHub Desktop.
Revisions
-
yuu-ito revised this gist
Dec 6, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ class Base(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) def add_instance_method(self, method): setattr(self, method.__name__, MethodType(method, self)) @@ -17,7 +17,7 @@ def say(self): p1 = Base(name="taro") p1.add_instance_method(say) p1.say() # --> 'taro: hi.' @@ -26,5 +26,5 @@ def say(self): p2 = Base(name="jiro") p2.add_instance_method(say) (p1.say(), p2.say()) # --> ('taro: hi.', 'jiro: hello') -
yuu-ito revised this gist
Dec 6, 2016 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,19 +7,24 @@ def __init__(self, **kwargs): def add_eigen_method(self, method): setattr(self, method.__name__, MethodType(method, self)) p0 = Base(name="zero") p0.say() # --> AttributeError: 'Base' object has no attribute 'say' def say(self): return "%s: hi." % self.name p1 = Base(name="taro") p1.add_eigen_method(say) p1.say() # --> 'taro: hi.' def say(self): return "%s: hello" % self.name p2 = Base(name="jiro") p2.add_eigen_method(say) (p1.say(), p2.say()) # --> ('taro: hi.', 'jiro: hello') -
yuu-ito revised this gist
Dec 6, 2016 . No changes.There are no files selected for viewing
-
yuu-ito created this gist
Dec 6, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ # https://docs.python.org/3.5/library/types.html from types import MethodType class Base(object): def __init__(self, **kwargs): self.__dict__.update(kwargs) def add_eigen_method(self, method): setattr(self, method.__name__, MethodType(method, self)) def say(self): return "%s: hi." % self.name p0 = Base(name="zero") p0.say() # --> AttributeError: 'Base' object has no attribute 'say' p1 = Base(name="taro") p1.add_eigen_method(say) p1.say() # --> 'taro: hi.' def say(self): return "%s: hello" % self.name p2 = Base(name="jiro") p2.add_eigen_method(say) (p1.say(), p2.say()) # --> ('taro: hi.', 'jiro: hello')