user8163366
user8163366

Reputation:

Python Interactive

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

Answers (1)

Ned Batchelder
Ned Batchelder

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

Related Questions