Hello, everyone, is Eiringu.
This article is a study memorandum of Fluent Python. It will be updated from time to time.
Honestly, I don't read through programming language textbooks seriously, but when I read a book that explains a language properly, I have proper coding skills and discoveries, unlike what I learn ad hoc in practice. .. So, I would like to make a note of what I was interested in with Fluent Python.
** P.8 Footnote **
In Python 2, you have to explicitly write
FrenchDeck (object)
, but in Python 3, this is the default.
...what...!?
P.45
2.7 list.sort and built-in function sorted
The list.sort method sorts the list in-place and does not make a copy. You can tell from the fact that None
is returned that you are modifying the target object without creating a new list.
(Omitted)
On the other hand, the built-in function sorted
creates a new list and returns it.
Hmm. I used to sort the list and save it to myself, so I used it as a reference.
2.9.1 Array
ʻArray.array is more efficient than
list when the list contains only numbers. ... (Omitted) ... Python arrays are as efficient as C arrays. ʻArray
is created by specifying the type code, because the type of the element stored in the array is supported by the internal C data type.
Lists also use only about the same type of data, so it would be more efficient if we started using arrays. I've never done a professional computer, but if you don't know this area, it won't work.
P. 76
my_dict.setdefault(key, []).append(new_value)
This is operationally the same as:
if key not in my_dict:
my_dict[key] = []
my_dict[key].append(new_value)
The method of using the ʻif` statement is easy to do, so be careful.
The extra is
b'\ xff \ xfe
. These are ** BOM ** * (byte-order mark) *, which represent the "little endian" byte order of the Intel CPU that performed the encoding. ~ Omitted ~ The standard stipulates that if the file is UTF-16 and there is no BOM there, then UTF-16BE (Big Endian) should be assumed. However, the Intel x86 architecture is little endian, and there are many little endian UTF-16s out there without a BOM. Endian issues only affect encodings that make up a word with multiple bytes, such as UTF-16 and UTF-32. One of the great advantages of UTF-8 is that it produces the same sequence of bytes regardless of the endianness of the machine. No BOM required. Nevertheless, some Windows applications add BOM to UTF-8 files (especially Notepad). Excel's UTF-8 file detection relies on BOM, without BOM assuming that the content is encoded in a Windows code page.
Ah, so if you load csv / tsv written in Python with Excel, the characters will be garbled. It doesn't recognize it as utf-8 by default.
Recommended Posts