Python Interactive
Is there a way to execute the method automatically from a python file after typing each command in python interactive mode?
For example: If I have a method that prints information about a file, but I don't want to constantly call that method, how can I make it output after each command in python interactive mode?
+3
user8163366
source
to share
1 answer
sys.displayhook is a function called to display values ββin an interactive interpreter. You can provide your own that does other things:
>>> 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!
>>>
+2
source to share