I have a python project I’m working on whereby instead of print statements I call a function say() so I can print information while in development and log information during production. However, I often forget this and put print statements in the code by mistake. Is there anyway to have the python program read its own source, and exit() if it finds any print statements outside of the function say()?
Share
This can be done using the
astmodule. The following code will find any calls of theprintstatement and also of theprint()function in case you are on Python 3 or Python 2 with theprint_functionfuture.The output of this example is: