Last active
May 30, 2018 09:36
-
-
Save rokujyouhitoma/1419251d41dfe90e8d52e7c2e50dd976 to your computer and use it in GitHub Desktop.
Pythonのクラスにexecで評価したfunctionオブジェクトを生やす
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
environment = {} | |
input1 = ''' | |
def say(self): | |
print("hello") | |
print(self.NAME) | |
self.hoo("say! hoo!") | |
''' | |
func_string = ''' | |
%s | |
environment[say.__name__] = say | |
''' % input1 | |
exec(func_string) | |
class X(object): | |
NAME = 'Im X' | |
def hoo(self, x): | |
print(x) | |
for k, v in environment.items(): | |
setattr(X, k, v) | |
if __name__ == '__main__': | |
x = X() | |
x.say() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment