Reputation:
Is there a way to execute a method automatically from a python file after each command input while in python interactive?
For example: If I have a method that prints information about file, but I do not want to call that method constantly, how can I make it output after each command in python interactive?
Upvotes: 3
Views: 99
Reputation: 375484
sys.displayhook is the function called to display values in the interactive interpreter. You can provide your own that performs other actions:
>>> 2+2
4
>>> original_display_hook = sys.displayhook
>>> def my_display_hook(value):
... original_display_hook(value)
... print("Hello there from the hook!")
...
>>> sys.displayhook = my_display_hook
>>> 2+2
4
Hello there from the hook!
>>>
Upvotes: 2