Python Language Incompatibilities moving from Python 2 to Python 3 Return value when writing to a file object


In Python 2, writing directly to a file handle returns None:

Python 2.x2.3
hi = sys.stdout.write('hello world\n')
# Out: hello world
# Out: <type 'NoneType'>

In Python 3, writing to a handle will return the number of characters written when writing text, and the number of bytes written when writing bytes:

Python 3.x3.0
import sys

char_count = sys.stdout.write('hello world 🐍\n')
# Out: hello world 🐍
# Out: 14

byte_count = sys.stdout.buffer.write(b'hello world \xf0\x9f\x90\x8d\n')
# Out: hello world 🐍
# Out: 17