-
-
Save akiko-pusu/d238317ebbf6696993b2f95d45434ac8 to your computer and use it in GitHub Desktop.
Pythonのインタラクティブモードで編集中のモジュールを再読み込みしたい ref: https://qiita.com/akiko-pusu/items/6e4c6114891dc83430a7
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
$ python sample.py | |
Hello World! |
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 Greeting: | |
def hello(self): | |
print("Hello World!") | |
def try_greeting(method_name='hello'): | |
greeting = Greeting() | |
if method_name is None: | |
method_name = 'hello' | |
eval('greeting.' + method_name)() | |
def main(): | |
try_greeting() | |
if __name__== "__main__": | |
main() |
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
$ python sample.py | |
Hello World! |
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
$ python -i sample.py | |
Hello World! | |
>>> main() | |
Hello World! |
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
>>> import sample | |
>>> sample.main() | |
Hello World! | |
# このあと、エディタ側で print("こんにちは!")に書き換えてsave | |
# でも変わらない... | |
>>> sample.main() | |
Hello World! | |
# importlibを使って再読み込みさせます | |
>>> import importlib | |
>>> importlib.reload(sample) | |
<module 'sample' from '/Users/akiko/work/convert_qiita/sample.py'> | |
# こんどは反映されました | |
>>> sample.main() | |
こんにちは! |
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
# 書き換えた状態でGreetingのインスタンスを作成 | |
>>> g = sample.Greeting() | |
>>> g.hello() | |
こんにちは!!!! | |
# このあと、print("こんにちは!!! うまくできてますか?") に書き換え | |
>>> importlib.reload(sample) | |
<module 'sample' from '/Users/akiko/work/convert_qiita/sample.py'> | |
# 再読み込みをさせますが、すでにインスタンス化されているものは、メソッドは変更が反映されません | |
>>> g.hello() | |
こんにちは!!!! | |
# 追加で新しく作ったほうは、変更が反映されています | |
>>> g2 = sample.Greeting() | |
>>> g2.hello() | |
こんにちは!!! うまくできてますか? | |
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
>>> import sample | |
>>> sample.main() | |
Hello World! | |
# このあと、エディタ側で print("こんにちは!")に書き換えてsave | |
# でも変わらない... | |
>>> sample.main() | |
Hello World! | |
# importlibを使って再読み込みさせます | |
>>> import importlib | |
>>> importlib.reload(sample) | |
<module 'sample' from '/Users/akiko/work/convert_qiita/sample.py'> | |
# こんどは反映されました | |
>>> sample.main() | |
こんにちは! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment