Difference between io.open vs open in python

Situation in Python3 according to the docs: io.open(file, *[options]*) This is an alias for the builtin open() function. and While the builtin open() and the associated io module are the recommended approach for working with encoded text files, this module [i.e. codecs] provides additional utility functions and classes that allow the use of a wider … Read more

__str__ versus __unicode__

__str__() is the old method — it returns bytes. __unicode__() is the new, preferred method — it returns characters. The names are a bit confusing, but in 2.x we’re stuck with them for compatibility reasons. Generally, you should put all your string formatting in __unicode__(), and create a stub __str__() method: def __str__(self): return unicode(self).encode(‘utf-8’) … Read more

Converting number in scientific notation to int

Behind the scenes a scientific number notation is always represented as a float internally. The reason is the varying number range as an integer only maps to a fixed value range, let’s say 2^32 values. The scientific representation is similar to the floating representation with significant and exponent. Further details you can lookup in https://en.wikipedia.org/wiki/Floating_point. … Read more

Reading binary data from stdin

From the docs (see here): The standard streams are in text mode by default. To write or read binary data to these, use the underlying binary buffer. For example, to write bytes to stdout, use sys.stdout.buffer.write(b’abc’). But, as in the accepted answer, invoking python with a -u is another option which forces stdin, stdout and … Read more