Pythonã§ãããã¯ã¼ã¯ä¸ã®ã³ã¼ããã¤ã³ãã¼ããã
impã¢ã¸ã¥ã¼ã«ã使ãã¨èªåã®importãè¨è¿°ã§ãããã ã¨ãããã¨ãããã£ãã®ã§ã試ãã«ã¯ã¦ãªãã¤ã¢ãªã¼ã«ç°¡åãªã½ã¼ã¹ã³ã¼ããã¢ãããã¼ããã¦ï¼°ï¼£ã®ãã¼ã«ã«ã«ãããã¡ã¤ã«ããã¤ã³ãã¼ããã¦é¢æ°ãå®è¡ããã¦ã¿ã¾ããã
å®è£
ã«ã¯http://antas.jp/blog/yamakawa/2008/03/python_1.htmlãå¼ç¨ããã¦ãããã¾ããã
ã¾ãã¯ãããã¯ã¼ã¯ä¸ã«ãããã½ã¼ã¹ãã¡ã¤ã«ã
def greeting(name): print "Hello, %s!" % name
次ã«ãã®ãã¡ã¤ã«ãã¤ã³ãã¼ãããã½ã¼ã¹ã³ã¼ãã
# coding: utf-8 import imp def import_from_string(name, code_str, default_dict = {}): # ref: http://antas.jp/blog/yamakawa/2008/03/python_1.html """ create module from string. """ mod = imp.new_module(name) mod.__dict__.update(default_dict) mod_dict = mod.__dict__ mod_dict["__source__"] = code_str if not mod_dict.has_key("__builtins__"): import __builtin__ mod_dict["__builtins__"] = __builtin__ # eval code_str in new module context. exec code_str in mod_dict return mod import urllib conn = urllib.urlopen("http://d.hatena.ne.jp/yatt/files/greeting.py?d=y") greeting = import_from_string("greeting", conn.read()) greeting.greeting("yatt")
å®è¡çµæ
Hello, yatt!
__import__ãä¸æ¸ããã¦ããããã¯ã¼ã¯ä¸ã«ãã¹ãã£ã³ã°ãããå¤é¨ã¢ã¸ã¥ã¼ã«ãééçã«ã¤ã³ãã¼ãã§ãããããªä»çµã¿ãä½ããã¨ä¾¿å©ããã