Open spaceone opened 1 year ago
Same confusion
Someone might use print()
for rapid debugging without an IDE or other development tools.
I'm not suggesting this, this is only one motivation of using this flake8
plugin.
If you're building an application that has structure and abstraction around logging, for example log levels, special formatting, perhaps even logging to places that aren't stdout, then print
would likely be an error as it wouldn't go through that logging code.
I think that's valuable only for certain applications, however the pprint
checks here are potentially more generally useful.
We're using this in our Django apps to force people to use Django's built-in logging feature (which comes with additional features regarding formatting, filtering, logging to a file instead of the console) instead of raw print statements.
I find these lints infuriating and prefer print
.
However, here's something to commit to memory next time you run into this!
import logging
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(message)s")
log.info('Hello world')
Output:
Hello world
The downside of logging
is the ceremony involved. The best practice I see for logging
typically involves:
For the patient, logging
is has some things going for it - it can be configured during a downstream user's application initialization:
logging.Formatter
)If the upstream package provides / supports it:
LoggerAdapter
)In addition to @tony's helpful comment, remember you can use sys.stdout.write(...)
in place of print()
for e.g. returning some value to stdout in a CLI. The benefit of this is that it's explicit that you are returning stuff to stdout.
Hello,
it would be nice to know why someone would want to prevent the use of
print()
? Maybe you can extend the README file to explain it?