I have asked A python program fails to execute in sublime text 3, but success in bash . I did some research and found it's necessary to start a new question.
In python2.7, the sys.getdefaultencoding()
is ascii:
In [1]: import sys
In [2]: sys.getdefaultencoding()
Out[2]: 'ascii'
In my opinion, print an obj is equal to print str(obj)
. If obj
is unicode, it will be encoded to ascii. For example(test.py):
#-*- encoding:utf-8 -*-
import sys
print sys.getdefaultencoding() # ascii
print "你好"
print u"你好" # should be an error occured: UnicodeEncodeError: 'ascii' codec...
But there is no error occured in ipython:
In [3]: print "你好"
你好
In [4]: print u"你好"
你好
Why does print an unicode obj in ipython occur no error? My understanding is not right?