@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Python: インタラクティブシェルで実行したコードの履歴を取得する

readline.get_history_item(n) でできる。

for i in range(readline.get_current_history_length()):
    print(readline.get_history_item(i+1))

Command history in interactive Python interpreter | by Amos Shapira | Medium

インタラクティブシェルのセッションを開始していろいろ実行した後から↑を実行しても、過去の履歴に遡って取得できる。