Last active
June 15, 2017 02:42
-
-
Save yuba/17a8c3985dc7e30a810ce6c5169736e5 to your computer and use it in GitHub Desktop.
クラスメソッド参照、インスタンスメソッド参照の取得と差し込み ref: http://qiita.com/yuba/items/6a167756fda660f8e759
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 C: | |
def __init__(self, n): | |
self.n = n | |
def m(self, x, y): | |
print('self.n:%s, x:%s, y:%s' % (self.n, x, y)) | |
i = C(1) | |
C.m # クラスメソッド参照 | |
i.m # インスタンスメソッド参照 | |
C.m(i, 5, 5) # クラスメソッド参照はselfを渡す必要あり | |
i.m(5, 5) # インスタンスメソッド参照はselfを渡す必要なし |
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
self.n:1, x:5, y:5 | |
self.n:1, x:5, y:5 |
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 C: | |
def __init__(self, n): | |
self.n = n | |
def m(self, x, y): | |
print('self.n:%s, x:%s, y:%s' % (self.n, x, y)) | |
def my_func(self, x, y): # selfを第一引数にとる関数 | |
self.m('%s兆' % (x * 1000), '%s兆' % (y * 1000)) | |
C.my_m = my_func | |
i = C(1) | |
i.my_m(5, 5) |
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
self.n:1, x:5000兆, y:5000兆 |
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 C: | |
def __init__(self, n): | |
self.n = n | |
def m(self, x, y): | |
print('n:%s, x:%s, y:%s' % (self.n, x, y)) | |
def my_func(x, y): # selfを引数にとらない関数 | |
C('???').m('%s兆' % (x * 1000), '%s兆' % (y * 1000)) | |
i = C(1) | |
i.my_m = my_func | |
i.my_m(5, 5) |
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 C: | |
def __init__(self, n): | |
self.n = n | |
def m(self, x, y): | |
print('n:%s, x:%s, y:%s' % (self.n, x, y)) | |
# ここですでにiというインスタンスが存在しているのがポイント | |
i = C(1) | |
self = i | |
def my_func(x, y): # selfを引数にとらない関数 | |
self.m('%s兆' % (x * 1000), '%s兆' % (y * 1000)) | |
i.my_m = my_func | |
i.my_m(5, 5) |
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
self.n:1, x:5000兆, y:5000兆 |
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 C: | |
def __init__(self, n): | |
self.n = n | |
def m(self, x, y): | |
print('n:%s, x:%s, y:%s' % (self.n, x, y)) | |
def wrap_m(self): | |
""" | |
与えられたオブジェクトのmというメソッドをいじって差し替えます | |
""" | |
m_ = self.m | |
# これが差し込むいたずら関数。クロージャでself(というかここではself.m)を知っている。 | |
def my_func(x, y): # selfを引数にとらない関数 | |
m_('%s兆' % (x * 1000), '%s兆' % (y * 1000)) | |
self.m = my_func | |
i1 = C(1) | |
i2 = C(2) | |
wrap_m(i1) # i1インスタンスのmメソッドをラップしちゃうぞ | |
i1.m(5, 5) | |
i2.m(5, 5) |
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
self.n:1, x:5000兆, y:5000兆 | |
self.n:2, x:5, y:5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment