How do you see the entire command history in interactive Python?

后端 未结 10 1791
不知归路
不知归路 2020-11-30 16:36

I\'m working on the default python interpreter on Mac OS X, and I Cmd+K (cleared) my earlier commands. I can go through them one by one using the arrow

10条回答
  •  离开以前
    2020-11-30 17:17

    Rehash of Doogle's answer that doesn't printline numbers, but does allow specifying the number of lines to print.

    def history(lastn=None):
        """
        param: lastn Defaults to None i.e full history. If specified then returns lastn records from history.
               Also takes -ve sequence for first n history records.
        """
        import readline
        assert lastn is None or isinstance(lastn, int), "Only integers are allowed."
        hlen = readline.get_current_history_length()
        is_neg = lastn is not None and lastn < 0
        if not is_neg:
            for r in range(1,hlen+1) if not lastn else range(1, hlen+1)[-lastn:]:
                print(readline.get_history_item(r))
        else:
            for r in range(1, -lastn + 1):
                print(readline.get_history_item(r))
    

提交回复
热议问题