1. Python 2 The reason analysis of no object __eq__ method
Python2.7:typeobject.c inside the PYBASEOBJECT_TYPE structure Tp_richcompare member is 0
And in the Python 3.x code, this member's value is Object_richcompare.
When Attr's lookup enters __getattr__, Python presses __mro__ to find the __mro__ of None (Nonetype, Object), in the C implementation, the former is Pynone_type, the latter is Pybaseob Ject_type. None of the two classes have __eq__ this key in the __dict__, so the lookup fails, raise Attributeerror.
But object.__eq__ can succeed, because the type of object is kind, and type.__dict__ has the key __eq__. A bit around. But it's a matter of knowing that Nonetype is a "special" type.
2. Python 2 terminal, file encoding problem
Under interactive command mode (Interactive Shell):
Import SYS
Under Windows, if your system is Simplified Chinese, the general output will be cp936
Import codecs>>> codecs.lookup ('cp936'). Name'GBK '
This is the encoding used in the Windows terminal. At print time, no matter what encoding the string source takes, the final output string must be encoded in the same way as the shell code, which means:
Print
Python will do such an action:
Codecs.encode (some_string, coding, errors='strict'
When Some_string is a Unicode string, coding is GBK, and some_string contains characters that are not in the GBK character set, Python throws a unicodeencodeerror.
for output to a file is similar, Python2 's open () function does not pass in encoding, open the file in ' W ' mode, and if the Unicode string is written, Python gets the current default encoding, The string is then written to the file with this encoding. Unfortunately, the "default encoding" is only Python's own default, in Objects/unicodeobject.c, a global variable unicode_default_encoding to represent the default encoding:
/* Default encoding to use and assume if NULL is passed as encoding parameter; it's initialized by _pyunicode_ini T (). Always use the pyunicode_setdefaultencoding () and pyunicode_getdefaultencoding () APIs to access this global. */staticchar unicode_default_encoding[1" ASCII"
This value has a C interface (pyunicode_setdefaultencoding) in Python to change the interface of the Python layer:
>>> sys.setdefaultencoding (coding)
However, you need to add the Py_using_unicode macro definition at compile time, and the official version of Windows does not have this feature.
So when calling to similar File.write (some_string), there is the first encoding process:
Codecs.encode (some_string, coding, errors='strict'
When Some_string is a Unicode string and contains characters that do not exist in the ASCII character set, a unicodeencodeerror is thrown.
Therefore, in order to solve this problem, you can deal with:
(1) Under Windows Shell:
print some_string.encode ('gbk', errors='ignore' #
(2) When writing to a file, there are two methods:
A. Ignoring non-existent characters, same as (1)
' W ' )>>> f.write (Some_string.encode ('gbk', errors='ignore ')
B. Byte write:
' WB ' )>>> f.write (Some_string.encode ('utf-8'))
A will lose character, B will not.
Two of the Python essays