This is a memo of the items that I personally learned by reading Effective Python. The "Foreword" of the book says "You can freely browse between items", but I will read them in order from the front. In addition, it seems that the second edition will be released on 2020/07/26, but please note that we will handle books released in 2016 here. Effective Python-59 items to improve Python programs (Japanese) Large book – 2016/1/23
Python programmers prefer to be explicit, choose simplicity over complexity, and maximize readability. Programmers familiar with other languages try to write Python as if it were C ++, Java, or the language they are most familiar with.
I read Chapter 1 later and then read back this preface, but I thought I couldn't grasp the "Python style" because some specifications and expressions I didn't know came out.
- Expressions and sentences (excerpts from some items)
Well, I feel like I've done this quite a bit. .. I'm sorry I won't do it anymore. ..
Slices also properly handle the start and end subscripts that cross list boundaries. Therefore, it is easy to write a code that sets the maximum length in consideration of the input sequence.
The following is feasible. It means that you can easily realize the code to extract up to 20 elements regardless of the sequence length by slicing.
a = ['a', 'b', 'c', 'd', 'e', 'f']
a[:20]
Execution result ['a', 'b', 'c', 'd', 'e', 'f']
I was ashamed to say that I didn't know the following. ..
The slice lengths to be assigned do not have to be the same. The slice values are preserved before and after the assignment.
I will try it.
a = ['a', 'b', 'c', 'd', 'e', 'f']
a[2:4] = [1, 2, 3, 4, 5]
print(a)
Execution result ['a', 'b', 1, 2, 3, 4, 5, 'e', 'f']
Really, it's done.
# somelist[start:end:stride]
a[2::2]
a[-2::-2]
a[-2:2:-2]
a[2:2:-2]
The point is that the stride part of the slice syntax is extremely confusing. (...) To avoid this problem, don't use stride with start and end subscripts.
In item 7, it is recommended to use list comprehension instead of map and filter, but list comprehension is not appropriate when there are many expressions. For example, in the following cases.
my_lists = [[[1, 2, 3], [4, 5, 6]]]
flat = [x for sublist1 in my_lists for sublist2 in sublist1 for x in sublist2]
The number of lines saved doesn't convince you of the hassle later.
This is really it.
When a break statement is executed in a loop, the else block is actually skipped.
I did not know. .. (I never tried to write it)
for i in range(3):
print('loop: %d' % i)
#Do not break inside the loop
else:
print('Else block!')
Execution result loop: 0 loop: 1 loop: 2 Else block!
for i in range(3):
print('loop: %d' % i)
if i == 1:
#Break in a loop
break
else:
print('Else block!')
Execution result loop: 0 loop: 1
It's true…! It says that the else block immediately after the loop should not be used because its behavior is not intuitive.
I personally wrote down the points that made me feel good.
I learned a lot from Chapter 1 because there were many sections that I could think of. I would like to summarize after this. Not all items are summarized, so if you are interested, please consider purchasing a book (not a turner)
Recommended Posts