Skip to content

Instantly share code, notes, and snippets.

@rokujyouhitoma
Last active May 30, 2018 09:36
Show Gist options
  • Save rokujyouhitoma/1419251d41dfe90e8d52e7c2e50dd976 to your computer and use it in GitHub Desktop.
Save rokujyouhitoma/1419251d41dfe90e8d52e7c2e50dd976 to your computer and use it in GitHub Desktop.
Pythonのクラスにexecで評価したfunctionオブジェクトを生やす
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