What's the best way I know? It doesn't look like Python after writing it.
I'd appreciate it if you could let me know if there's another way
import sys
#1.
print >> sys.stderr, 'spam'
#2.
sys.stderr.write('spam\n')
#3.
from __future__ import print_function
print('spam', file=sys.stderr)
Among the methods you wrote, considering the readability and Python version, I think the second method is the best.
The first method is not supported by python3.
The third method is also using the __future___
module.
Here's how I came up with it.
from __future__ import print_function
def warning(*objs):
print("WARNING: ", *objs, file=sys.stderr)
© 2024 OneMinuteCode. All rights reserved.