forked from mikeckennedy/talk-python-transcripts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path025_effective_python.vtt
2777 lines (1851 loc) · 84.5 KB
/
025_effective_python.vtt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
WEBVTT
00:00:00.001 --> 00:00:05.960
What if you could bottle up all the wisdom and hard-fought experience of many expert Python developers and power up your skills?
00:00:05.960 --> 00:00:10.240
That's what Brett Slatkin did, and he put it in his new book, Effective Python.
00:00:10.240 --> 00:00:16.720
This is episode number 25 of Talk Python to Me, recorded Wednesday, August 12, 2015.
00:00:16.720 --> 00:00:46.700
Welcome to Talk Python to Me, a weekly podcast on Python, the language, the library's,
00:00:46.720 --> 00:00:48.280
the ecosystem, and the personalities.
00:00:48.280 --> 00:00:50.100
This is your host, Michael Kennedy.
00:00:50.100 --> 00:00:52.340
Follow me on Twitter, where I'm @mkennedy.
00:00:52.340 --> 00:00:56.220
Keep up with the show and listen to past episodes at talkpython.fm.
00:00:56.220 --> 00:00:59.260
And follow the show on Twitter via at Talk Python.
00:00:59.260 --> 00:01:02.560
This episode is brought to you by Hired and CodeChip.
00:01:02.560 --> 00:01:07.760
Thank them for supporting the show on Twitter via at Hired underscore HQ and at CodeChip.
00:01:07.760 --> 00:01:10.880
It's another week with a book giveaway.
00:01:10.880 --> 00:01:14.360
And just like last week's, this one is a really excellent one.
00:01:14.880 --> 00:01:20.220
Make sure you're a friend of the show and you'll be in the running to win the free e-book version of Effective Python.
00:01:20.220 --> 00:01:25.380
Just visit talkpython.fm, click on Friends of the Show, and boom, winning happens.
00:01:25.380 --> 00:01:27.300
Well, sometimes, if you're lucky.
00:01:27.300 --> 00:01:30.620
Now let me introduce Brett so we can get right to the show.
00:01:30.620 --> 00:01:33.700
Brett Slatkin is the author of Effective Python.
00:01:33.700 --> 00:01:37.880
He's the engineering lead and co-founder of Google Consumer Surveys.
00:01:37.900 --> 00:01:44.180
He formerly worked on Google App Engine, the PubSub Hubbub protocol, and managing Google's fleet of servers.
00:01:44.180 --> 00:01:50.920
Outside his day job, he works on open source tools and writes about software, bicycles, and other topics on his personal website.
00:01:50.920 --> 00:01:53.040
And today, he lives in San Francisco.
00:01:53.040 --> 00:01:55.200
Brett, welcome to the show.
00:01:55.200 --> 00:01:57.000
Thanks for having me, Michael.
00:01:57.500 --> 00:02:01.720
Yeah, I'm really excited to talk to you about your book and the other stuff you have going on with Python.
00:02:01.720 --> 00:02:06.600
We're going to talk about this book that you recently released called Effective Python.
00:02:06.600 --> 00:02:11.380
And I was a big fan of the Effective series back in the C++ days.
00:02:11.380 --> 00:02:14.340
And so when I saw your book title, I was like, oh, this is going to be awesome.
00:02:14.340 --> 00:02:16.400
So it's going to be a fun conversation.
00:02:17.140 --> 00:02:17.560
Yeah, definitely.
00:02:17.560 --> 00:02:23.820
And I similarly, Effective C++ was the best programming book that I ever had.
00:02:23.820 --> 00:02:27.060
And so I totally agree with that.
00:02:27.060 --> 00:02:31.040
So that was 1997 or 1998 when that came out when I first read it.
00:02:31.040 --> 00:02:38.300
And being able to make the Python version of that was really a great honor for me to be able to write.
00:02:38.300 --> 00:02:39.700
Yeah, I bet it was.
00:02:39.700 --> 00:02:40.380
That's great.
00:02:40.880 --> 00:02:47.540
Before we get into the details of your book, though, maybe we could just talk a bit about how you got to where you are.
00:02:47.540 --> 00:02:49.340
How did you get into programming in Python?
00:02:49.340 --> 00:02:55.760
Yeah, so I won't bore you with the super long details of my history of writing code.
00:02:55.760 --> 00:02:58.740
But I've been writing code since I was about 10 or 11 years old.
00:02:58.740 --> 00:03:02.700
My first job out of college was at Google.
00:03:02.700 --> 00:03:06.400
And I showed up for my first day of work.
00:03:06.400 --> 00:03:10.140
And they said, OK, here's this Python code base.
00:03:10.220 --> 00:03:11.200
You need to make it better.
00:03:11.200 --> 00:03:12.580
Here's a book.
00:03:12.580 --> 00:03:15.240
And Alex Martelli sits right there.
00:03:15.240 --> 00:03:16.120
Figure it out.
00:03:16.120 --> 00:03:18.740
So I had seen Python.
00:03:18.740 --> 00:03:20.580
I hadn't written any Python at all.
00:03:20.580 --> 00:03:28.220
And I went from basically knowing nothing about Python and then actually writing a book about it in about 10 years, which is pretty funny.
00:03:28.220 --> 00:03:29.180
Yeah, that's great.
00:03:29.180 --> 00:03:32.080
The student becomes the master sort of thing, right?
00:03:32.080 --> 00:03:32.260
Yeah.
00:03:32.260 --> 00:03:33.480
I hope so.
00:03:33.480 --> 00:03:34.680
I hope it's worthwhile.
00:03:34.680 --> 00:03:40.360
Well, I think what was funny is that my first impression of Python was from the BitTorrent code base.
00:03:40.360 --> 00:03:44.520
Because Bram Cohen had had this post all about Python and how it makes him more efficient.
00:03:44.520 --> 00:03:46.100
And that's why BitTorrent's so awesome.
00:03:47.740 --> 00:03:50.520
I don't think people realize that BitTorrent was written by Python.
00:03:50.520 --> 00:03:58.820
But then like Zope and Plone, I thought were just really, they were code bases that I was very unhappy with using when I tried Python for the first time.
00:03:58.820 --> 00:03:59.800
And that kind of turned me off.
00:03:59.800 --> 00:04:04.900
So then when I showed up at Google and they said, OK, you're using Python, I was cautiously optimistic.
00:04:06.180 --> 00:04:09.280
But it ended up turning out real well.
00:04:09.280 --> 00:04:15.020
And so I can just go real quick, just the kind of projects that I used Python for at Google.
00:04:15.020 --> 00:04:16.900
Yeah, people would love to hear that.
00:04:16.900 --> 00:04:23.040
My first job there was kind of doing janitorial services for data centers.
00:04:23.040 --> 00:04:26.260
Google has a bunch of machines in their data centers.
00:04:26.260 --> 00:04:28.000
Those machines have a life cycle.
00:04:28.000 --> 00:04:28.740
They're built.
00:04:28.740 --> 00:04:29.780
They're repaired.
00:04:29.780 --> 00:04:31.420
They break.
00:04:31.860 --> 00:04:37.260
And I worked on a bunch of the tools to help that life cycle, which are primarily written in Python.
00:04:37.260 --> 00:04:50.820
And I got to scale that out into Google's kind of worldwide network of data centers, a lot of system level code, a lot of networking, kind of database accessing, all kinds of stuff like that, workflows.
00:04:50.820 --> 00:04:53.500
So that was my first kind of big role.
00:04:53.500 --> 00:05:00.560
I did some other things similar to that around security and securing the machines in a data center.
00:05:01.620 --> 00:05:04.760
And then shortly after that, that was a couple of years.
00:05:04.760 --> 00:05:09.980
And then shortly after that, I heard of this really cool project that was being created called App Engine that had just started.
00:05:09.980 --> 00:05:14.400
And I thought it really had a lot of potential.
00:05:14.400 --> 00:05:16.340
And so I went and talked to that team.
00:05:16.340 --> 00:05:19.820
I think it was four people at that point, the original founding team.
00:05:19.820 --> 00:05:22.160
And I was like, what can I do to get on this team?
00:05:22.160 --> 00:05:23.920
How can I work on this?
00:05:23.920 --> 00:05:36.560
And in my 20% time, I built the App Engine Dev App Server, which is, I don't know if you've ever used App Engine, but you spend a lot of your time using this development tool to actually write your apps.
00:05:37.460 --> 00:05:38.580
And so, yes, I built that.
00:05:38.580 --> 00:05:39.760
And they're like, oh, this is pretty good.
00:05:39.760 --> 00:05:41.560
And then they invited me to join the team.
00:05:41.560 --> 00:05:49.740
And then I helped build App Engine out and launch it, which was, so it's, you know, platform as a service, Google's cloud kind of system.
00:05:49.740 --> 00:05:51.860
Yeah, that's, it's really cool.
00:05:51.960 --> 00:05:55.660
And was Python the very first language or was it Python and Go?
00:05:55.660 --> 00:05:56.320
What was it?
00:05:56.320 --> 00:05:58.500
It launched with just a restricted set, right?
00:05:58.500 --> 00:05:58.760
Yeah.
00:05:58.760 --> 00:06:00.300
So Python was the first language.
00:06:00.300 --> 00:06:02.280
Java came like a year and a half or two years later.
00:06:02.280 --> 00:06:09.160
And then, and then Go came a little bit after, like maybe two, two or three years after that.
00:06:09.160 --> 00:06:16.240
I think the first language is actually supposed to be JavaScript, server-side JavaScript in the old Netscape, LiveScript scheme of things.
00:06:17.440 --> 00:06:25.980
But in 2008, that seemed like a completely ridiculous proposition, which is pretty funny looking at all the Node.js stuff that's happened in the last few years with V8.
00:06:25.980 --> 00:06:27.140
This is way before V8.
00:06:27.140 --> 00:06:29.300
JavaScript is still really slow.
00:06:29.300 --> 00:06:35.740
And so anyway, so they went with Python because it was one of the main languages that Google had used.
00:06:35.740 --> 00:06:37.660
And Google had a lot of expertise in Python in general.
00:06:37.660 --> 00:06:42.560
So on the App Engine team, I spent a lot of time working on Python infrastructure, Python APIs,
00:06:43.280 --> 00:06:50.240
just kind of the feel of what it was like to write apps on top of App Engine using Python and fix a lot of bugs,
00:06:50.240 --> 00:07:01.320
vetted new APIs to go out to make sure they felt good, built infrastructure around task queues and MapReduce and offline processing and all kinds of different things.
00:07:01.320 --> 00:07:03.360
Did that for a few years.
00:07:04.620 --> 00:07:14.140
And then also started this project called PubSubHubbub in there, which was a real-time RSS project for making RSS feeds real-time,
00:07:14.140 --> 00:07:22.700
which is kind of hilarious also in hindsight now since that's pretty – it's kind of like saying I made something to design pogs or something like that.
00:07:22.700 --> 00:07:23.400
It's kind of old.
00:07:23.400 --> 00:07:25.000
Yeah, that's pretty funny.
00:07:25.000 --> 00:07:27.840
But that was the Google Reader days and all that kind of stuff, right?
00:07:27.840 --> 00:07:29.140
Yes, the good old days.
00:07:29.140 --> 00:07:29.640
Yeah.
00:07:29.740 --> 00:07:30.880
I kind of miss that thing still.
00:07:30.880 --> 00:07:33.180
I do too, all the time.
00:07:33.180 --> 00:07:38.160
I've been using News Blur recently, which I believe is also a Python app written in Django.
00:07:38.160 --> 00:07:40.940
But, you know, anyway, I miss Reader.
00:07:40.940 --> 00:07:42.180
Yeah, definitely.
00:07:42.180 --> 00:07:44.900
So that is a bunch of really cool stuff.
00:07:44.900 --> 00:07:49.080
And we might have to come back and talk about that at some later date in more detail.
00:07:49.080 --> 00:07:49.840
That'd be really fun.
00:07:49.840 --> 00:07:51.900
But let's talk about your book today.
00:07:51.900 --> 00:07:53.740
It's called Effective Python.
00:07:53.740 --> 00:07:54.720
What's the subtitle?
00:07:55.640 --> 00:07:58.600
It's 59 Specific Ways to Write Better Python.
00:07:58.600 --> 00:08:04.660
It's kind of based in this – the heritage of the Effective series, which, as we kind of hinted at the beginning,
00:08:04.660 --> 00:08:08.820
I think that started out with Scott Myers and Effective C++, right?
00:08:08.820 --> 00:08:10.700
Yeah, he invented the format.
00:08:10.700 --> 00:08:17.200
And, you know, he's really the guy who came up with this way of educating people.
00:08:17.200 --> 00:08:19.460
And all the other books are in his style.
00:08:20.200 --> 00:08:20.780
Yeah, that's cool.
00:08:20.780 --> 00:08:25.700
And so I remember when I was learning C++, I could sort of do stuff with it.
00:08:25.700 --> 00:08:27.060
You know, I could write code and so on.
00:08:27.060 --> 00:08:34.000
But after I read his book, I felt like it really took my understanding and effectiveness to a new level.
00:08:34.000 --> 00:08:37.600
And so you're kind of trying to bring this to the Python developers, right?
00:08:37.600 --> 00:08:39.020
Definitely.
00:08:39.020 --> 00:08:41.300
Yeah, that's the goal of the book.
00:08:41.300 --> 00:08:43.620
It's a bit of an audacious goal, but yeah.
00:08:44.360 --> 00:08:49.260
Yeah, I mean, I think it's really simple is that, like, I read an introductory Python book.
00:08:49.260 --> 00:08:54.880
This Effective Python is the book I would have given to myself as a second book if I had had the chance.
00:08:54.880 --> 00:08:59.620
So there was a gap in my knowledge that took me years and years and years to fill.
00:08:59.620 --> 00:09:05.380
And this book is kind of a way to shortcut and get all that practical knowledge without having to actually pay the dues.
00:09:05.380 --> 00:09:09.260
So I wish I had had this book when I was starting out.
00:09:09.960 --> 00:09:14.420
You know, we talk a lot about the concept of Pythonic code and idiomatic Python and stuff.
00:09:14.420 --> 00:09:16.900
And there's a lot of that concept in there.
00:09:16.900 --> 00:09:26.580
You know, like, this is the way you should do things in Python, even if you already know if you could accomplish it using your old idioms from, say, Java or C# or something.
00:09:26.580 --> 00:09:35.120
But then there's another part, another angle to it that's just sort of broadening your horizon, I think.
00:09:35.120 --> 00:09:37.120
So it's not just about writing Pythonic code.
00:09:37.120 --> 00:09:47.380
And I'm thinking of things like how you would use, say, subprocess to manage child processes and parallelism from your Python app, right?
00:09:47.380 --> 00:09:51.560
That's not technically a Pythonic thing, but it definitely ups your game and what you can do.
00:09:51.560 --> 00:09:57.700
Yeah, I think, you know, a lot of the advice is pure language construct kind of stuff.
00:09:58.520 --> 00:10:01.160
And then other things are like, yeah, the subprocess is item.
00:10:01.160 --> 00:10:06.920
But, you know, it's important to remember that one thing that they say about Python is, you know, batteries are included, right?
00:10:06.920 --> 00:10:14.920
And so, you know, the difference between Python as a language and Python as an ecosystem or Python as a set of libraries that you can use.
00:10:15.400 --> 00:10:23.800
When people think of Python, they think of all of it together, both the syntax, the language features that it has, and then the libraries that you know you can rely on to always be there.
00:10:23.800 --> 00:10:34.580
So those are tools you should have in your toolbox that are part of being a Python programmer, just like, you know, the collections API if you're a Java programmer, because it's basically all you write all the time.
00:10:34.800 --> 00:10:35.760
Yeah, absolutely.
00:10:35.760 --> 00:10:43.140
I have many friends that work in other languages, and they often want to compare their language to Python.
00:10:43.140 --> 00:10:54.100
And like you said, I think that's not even a conversation you can begin to have if you don't think about the entire standard library and the 60,000 PyPI packages.
00:10:54.100 --> 00:10:56.040
And, you know, you've got to take it as a whole, right?
00:10:56.040 --> 00:10:58.720
It's not just, here's how you do properties here and properties there.
00:10:58.720 --> 00:11:01.280
So this is either better or less good, right?
00:11:02.000 --> 00:11:02.620
Yeah, totally.
00:11:02.620 --> 00:11:10.260
I mean, a lot of these things, you have so much, I like to use the word leverage, you know, to describe, you get to stand on the shoulders of giants.
00:11:10.260 --> 00:11:11.400
There's a lot of different ways to say it.
00:11:11.400 --> 00:11:17.360
But yeah, things like NumPy, as an example of a library, it's a whole ecosystem unto itself.
00:11:17.360 --> 00:11:22.700
And trying to do the equivalent in Java or C++ is extremely difficult.
00:11:22.700 --> 00:11:24.040
Yeah, that's for sure.
00:11:24.600 --> 00:11:36.300
So I thought one way that would be fun to have a conversation about your book is kind of go from section to section or chapter to chapter and pick out a few interesting pieces of guidance from each one of them.
00:11:36.300 --> 00:11:37.340
Yeah, definitely.
00:11:37.340 --> 00:11:38.000
Yeah, cool.
00:11:38.000 --> 00:11:42.600
So you broke your book into six parts, and we'll just take them one at a time.
00:11:42.600 --> 00:11:47.980
So the first one is this concept of sort of the libraries and Pythonic thinking, right?
00:11:48.340 --> 00:11:49.440
Yeah, that's the first part.
00:11:49.440 --> 00:11:53.800
It's actually eight separate chapters is how many there are, because it keeps going.
00:11:53.800 --> 00:11:58.020
But yeah, the first one is Pythonic thinking.
00:11:58.020 --> 00:11:59.080
Excellent.
00:11:59.080 --> 00:12:00.560
So why'd you start with that?
00:12:01.860 --> 00:12:07.300
Yeah, I think that it's kind of like the strange universe of Python syntax.
00:12:07.300 --> 00:12:09.020
You have to get used to doing things.
00:12:09.020 --> 00:12:19.020
Even simple expressions like, I remember when I first started writing Python, and the not operator, as opposed to using the bang or exclamation point, getting used to where that not goes.
00:12:19.020 --> 00:12:25.180
And things like is not, as opposed to, you could do A is not B, or you could do not A is B.
00:12:26.220 --> 00:12:30.920
And, you know, understanding that the Pythonic way is A is not B. That's how you'd say it.
00:12:30.920 --> 00:12:37.400
So you really need this base to start with, so that all the other things make sense or are in context.
00:12:37.400 --> 00:12:45.500
Right. I also think it's probably a good way to kind of shock the system a little bit to prepare them for, like, this is a different world, probably, than you're coming from.
00:12:45.500 --> 00:12:46.000
Yeah.
00:12:46.320 --> 00:12:48.600
And so get prepared to think differently.
00:12:48.600 --> 00:12:56.940
Yeah, it's to open, I think it's a good way to say it, because I think it's, if you try to fit it into the way you're writing other languages, you're going to have a bad time.
00:12:56.940 --> 00:13:00.100
And you really need to embrace the Pythonic way of doing it.
00:13:00.100 --> 00:13:04.740
I had one person told me once that my C++ code looks like Python.
00:13:04.740 --> 00:13:06.900
I thought that was a really great compliment, I guess.
00:13:08.100 --> 00:13:10.600
But you don't really want your Python to look like C++, right?
00:13:10.600 --> 00:13:11.060
Yeah.
00:13:11.060 --> 00:13:12.160
Absolutely.
00:13:12.160 --> 00:13:13.680
Cool.
00:13:13.680 --> 00:13:21.920
So the very first thing that you talk about, literally the first piece of guidance, is to know what version of Python you're using.
00:13:21.920 --> 00:13:25.580
And I do a lot of training in Python, and it's always a problem.
00:13:25.580 --> 00:13:33.040
Someone's always running the wrong version of Python, or even worse, they're trying to use a package and they install it into the wrong version of Python,
00:13:33.040 --> 00:13:35.120
and then they're running the right one or something like that, right?
00:13:35.740 --> 00:13:39.120
Yeah, that's a big problem, especially with the Python 3 move that's going on.
00:13:39.120 --> 00:13:41.380
You know, you can get confused really easily.
00:13:41.380 --> 00:13:46.320
And then a lot of the features that people like to use, like even with statements, are actually more recent developments.
00:13:46.320 --> 00:13:52.280
So if you're on some old version, some versions of macOS are like running Python 2.5, and you don't know it,
00:13:52.280 --> 00:13:54.920
and you try to do simple things like a with statement, it doesn't work.
00:13:54.920 --> 00:13:57.060
And so that can be very surprising.
00:13:57.060 --> 00:13:58.260
So it's always good to check your assumptions.
00:13:58.260 --> 00:13:59.440
Yeah, absolutely.
00:13:59.440 --> 00:14:02.340
So sort of level set, all right, we all know what we're talking about.
00:14:02.340 --> 00:14:03.500
Now we can move forward, right?
00:14:03.500 --> 00:14:05.040
Yeah, exactly.
00:14:05.380 --> 00:14:12.020
So what I think probably a lot of people are familiar with, but some are not, is sort of working with sequences.
00:14:12.020 --> 00:14:18.860
And you say you should prefer using list comprehensions over like chaining a map and a filter call together.
00:14:18.860 --> 00:14:20.180
Yeah.
00:14:21.000 --> 00:14:31.960
Yeah, so I think if you, yeah, I think the story, so it's interesting, because if you look at the history of Python, people will talk about how, oh, I like Python, because it's kind of functional, but it's kind of object oriented, and it's kind of scripty.
00:14:33.240 --> 00:14:36.600
And so you'll still see examples or guides today.
00:14:36.600 --> 00:14:40.880
You'll be searching around the internet, and you'll find some old guide on active state from like 2006.
00:14:40.880 --> 00:14:42.960
And it's like, here's how you should use map and filter.
00:14:42.960 --> 00:14:47.280
And, or even 2003, like, you know, a long time ago.
00:14:47.520 --> 00:14:53.880
And those are useful tools, sometimes they're useful, but the list comprehension syntax is just so much more pithy.
00:14:54.780 --> 00:15:07.600
I think that Python over the years has added more and more tools to maximize the readability of code, and to minimize the visual noise, the extra parentheses and brackets and various symbols that you need to express something.
00:15:07.600 --> 00:15:18.200
And so I think that map and filter are kind of just antiquated tools that are in the language, because they're hard to take out, because they're, you know, kind of keywords, built in functions.
00:15:18.200 --> 00:15:22.900
If I had my way, I would just take them out, because I think list comprehensions are better.
00:15:22.900 --> 00:15:36.360
This episode is brought to you by Hired.
00:15:36.360 --> 00:15:42.820
Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
00:15:42.820 --> 00:15:51.960
Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
00:15:52.420 --> 00:15:58.340
Typically, candidates receive five or more offers in just the first week, and there are no obligations, ever.
00:15:58.340 --> 00:16:00.420
Sounds pretty awesome, doesn't it?
00:16:00.420 --> 00:16:02.460
Well, did I mention there's a signing bonus?
00:16:02.460 --> 00:16:06.580
Everyone who accepts a job from Hired gets a $2,000 signing bonus.
00:16:06.580 --> 00:16:10.920
And as Talk Python listeners, it gets way sweeter.
00:16:10.920 --> 00:16:18.480
Use the link Hired.com slash Talk Python To Me, and Hired will double the signing bonus to $4,000.
00:16:19.380 --> 00:16:20.200
Opportunity's knocking.
00:16:20.200 --> 00:16:23.800
Visit Hired.com slash Talk Python To Me and answer the call.
00:16:34.020 --> 00:16:45.120
But yeah, the main thing is that, you know, if you have two ways of doing something, I think the Pythonic way of doing it is always the more clear and more explicit and obvious way of doing it.
00:16:45.120 --> 00:16:48.420
And so yeah, so that's why I think list comprehensions are definitely the way to go.
00:16:48.420 --> 00:16:50.040
Yeah, that's cool.
00:16:50.040 --> 00:16:50.780
I totally agree.
00:16:51.580 --> 00:16:56.560
It's not exactly a more declarative way of programming, but it's closer, right?
00:16:56.560 --> 00:16:57.200
That's nice.
00:16:57.200 --> 00:16:58.520
Yeah, it's more obvious.
00:16:58.520 --> 00:17:05.360
Sort of related to that, one of your other pieces of guidance was that you should use generators for large list comprehensions.
00:17:05.360 --> 00:17:06.420
Yeah.
00:17:06.420 --> 00:17:09.580
It's so easy to switch from one to the other, but when should I use one?
00:17:09.580 --> 00:17:10.280
When shouldn't I?
00:17:10.280 --> 00:17:11.260
What's the story of that?
00:17:11.920 --> 00:17:16.240
Yeah, so I think that, you know, Python can use a lot of memory is one big thing.
00:17:16.240 --> 00:17:24.380
And depending on what you're trying to do, if you're dealing with a small data set that entirely fits within memory, then you don't have to really think about this.
00:17:24.380 --> 00:17:27.980
You can just read everything as a list and just work on lists.
00:17:27.980 --> 00:17:35.000
So if you're reading a file, like a CSV file, and you want to deal with it line by line or something like that, then reading the whole thing into memory is fine.
00:17:35.000 --> 00:17:44.880
But what always happens is you end up wanting to do something a little bit bigger than you expected, especially with a lot of the data processing stuff that people are doing in Python these days.
00:17:44.880 --> 00:17:52.660
And so I'm trying to change the culture a little bit to say, hey, why not use a generator if you can?
00:17:52.660 --> 00:17:56.360
Because if you use a generator, then all of these memory problems go away.
00:17:57.740 --> 00:17:59.520
A generator returns one item at a time.
00:17:59.520 --> 00:18:01.760
It doesn't fully materialize the list.
00:18:01.760 --> 00:18:07.420
So the total memory space that's occupied is just the last thing you return from the generator.
00:18:07.420 --> 00:18:10.140
The rest of it gets cleaned up, garbage collected.
00:18:10.140 --> 00:18:11.500
Yeah, that's fantastic.
00:18:11.500 --> 00:18:17.080
Especially if you're like chaining one to another to another, and they're kind of building up.
00:18:17.080 --> 00:18:19.120
It's really efficient.
00:18:19.120 --> 00:18:27.560
Yeah, and to that point, I mean, I think that if you start by at the base level, like if you want to switch to generator code, it's difficult because you have to, you have to,
00:18:27.560 --> 00:18:30.560
put generators all the way down to the leaves of your call stacks.
00:18:30.560 --> 00:18:35.660
But if you start with generators at the leaves, then it's very easy to start saying, hey, you know, I'm going to turn this into a streaming generator.
00:18:35.660 --> 00:18:38.760
And so creating those cascades of generators becomes very straightforward.
00:18:39.260 --> 00:18:48.520
And so you can get quick, you know, very fast executing code that's easy to follow and uses a low amount of memory very, you know, very cheaply.
00:18:48.520 --> 00:18:50.060
And it's very readable.
00:18:50.060 --> 00:18:52.920
So that's why I suggest that.
00:18:53.040 --> 00:19:04.280
So the final language one that I wanted to sort of point out is something that I always thought was weird is the whole concept of the else statement on a loop.
00:19:04.280 --> 00:19:04.900
Yeah.
00:19:05.760 --> 00:19:12.120
And something I recently learned is that try except finally also supports else.
00:19:12.120 --> 00:19:13.200
It does.
00:19:13.200 --> 00:19:13.540
Yeah.
00:19:13.540 --> 00:19:14.000
Yeah.
00:19:14.000 --> 00:19:15.420
And you have really interesting guidance.
00:19:15.420 --> 00:19:20.940
You say, you know what, these, this else statement on loops, maybe not so much, but on exception handling, it's very cool.
00:19:20.940 --> 00:19:22.380
It's like an alternative to the catch.
00:19:22.760 --> 00:19:23.720
Yeah, definitely.
00:19:23.720 --> 00:19:24.200
Yeah.
00:19:24.200 --> 00:19:31.840
So I think else, else makes a lot of sense for try except finally, because you're basically saying, you know, try to do this.
00:19:31.840 --> 00:19:33.720
If an exception happens, do this.
00:19:33.720 --> 00:19:36.660
And if no exception happens, then do this other thing.
00:19:36.660 --> 00:19:40.140
And that's what the else block is on a, on an exception, kind of try except.
00:19:40.140 --> 00:19:45.720
Uh, and so that makes things really clear, clearly delineated what will and will not happen inside your exception handling.
00:19:45.720 --> 00:19:52.300
Um, and when you're doing exception handling, you want the, the try block to be as small as possible to narrow what you're catching.
00:19:52.300 --> 00:19:56.100
So that, that exceptions you didn't plan to catch are raised back up.
00:19:56.100 --> 00:19:57.460
That's, that's like a really important thing.
00:19:57.460 --> 00:19:58.640
Yeah, that makes sense.
00:19:58.640 --> 00:20:11.360
And the other thing is, you know, you might think, well, just the last bit of code in your try bit, your try try block could be what would go in the else statement, but that doesn't account for like early returns.
00:20:11.360 --> 00:20:22.280
And so, you know, if you were to do, try to do some stuff, if this, oh, just return, it would still run that else, but then you'd have to be really careful, you know, trying to put that cleanup all over the try.
00:20:22.280 --> 00:20:23.920
So yeah, it's really nice.
00:20:23.920 --> 00:20:24.560
Yeah, it's nice.
00:20:24.640 --> 00:20:28.700
And yeah, you can put like final cleanup stuff in the finally, and that'll always run if you have an early return.
00:20:29.000 --> 00:20:32.620
And if you have an else with an early return, that'll also still run the finally.
00:20:32.620 --> 00:20:33.920
So it's, it's super nice.
00:20:34.720 --> 00:20:36.520
Going to that back to that for loop part of it.
00:20:36.520 --> 00:20:37.960
Yeah, I think that's one of the big things.
00:20:37.960 --> 00:20:40.980
Like when people first learn about that in Python, they're like, oh, this is so great.
00:20:40.980 --> 00:20:43.900
I can, I can use it for doing this and that.
00:20:43.900 --> 00:20:47.680
And it's, there's really, yeah, I think it's something to avoid.
00:20:47.680 --> 00:20:56.080
It's kind of like, it's a shiny new toy and you want to use all the different parts of it for you, you know, and you have to like stab yourself in the eye before you realize that you're getting, that wasn't a good idea.
00:20:57.360 --> 00:20:58.020
That thing hurts.
00:20:58.020 --> 00:20:58.680
Yeah, it hurts.
00:20:58.680 --> 00:20:59.320
Like, oh, it's short.
00:20:59.320 --> 00:21:00.000
It's pointy.
00:21:00.000 --> 00:21:00.840
I shouldn't touch that.
00:21:00.840 --> 00:21:10.940
And I was talking to, I was talking to Guido about this one in particular because he said, you know, cause I was asking him what he thinks about this one.
00:21:10.940 --> 00:21:12.280
Cause it's kind of, it's kind of tough.
00:21:12.280 --> 00:21:17.180
I had worked with Guido on App Engine for a few years and, so, and I see him at PyCon from time to time.
00:21:17.180 --> 00:21:20.960
And so, you know, I felt bad by saying like, hey, you shouldn't use this part of the language.
00:21:20.960 --> 00:21:23.340
I just, you know, wanted to make sure it wasn't ridiculous to him.
00:21:23.980 --> 00:21:29.560
And, you know, he said that it's actually an implementation detail of how the Python, the CPython runtime is implemented.
00:21:29.560 --> 00:21:37.600
Um, the way that the for else block, the else with loops works has to do with the way these go to statements actually work in the Python interpreter.
00:21:37.600 --> 00:21:47.060
Uh, and so if you, if you, if you wrote that code, then it makes perfect sense because the else has to do with the way the certain switch or go to statement works.
00:21:47.060 --> 00:21:48.440
Um, it's switch statements.
00:21:48.440 --> 00:21:48.600
Sorry.
00:21:48.600 --> 00:21:49.260
It's not a go to thing.
00:21:49.260 --> 00:21:49.680
It's a switch.
00:21:49.680 --> 00:21:53.960
And, but if you don't have that mental model, then it makes no sense.
00:21:53.960 --> 00:22:04.940
And, you know, I think that, so for, for Guido and maybe other people who've hacked on the core, they just get it and it makes sense to them, but everyone else they have, it does the opposite of what you would expect.
00:22:04.940 --> 00:22:08.340
And so just because of that, I think it's too sharp and I think it's something you should avoid.
00:22:08.340 --> 00:22:13.020
Talk Python To Me is partially supported by our training courses.
00:22:13.020 --> 00:22:19.220
Do you want to learn Python, but you can't bear to subscribe to yet another service at Talk Python Training?
00:22:19.580 --> 00:22:20.560
We hate subscriptions too.
00:22:20.560 --> 00:22:21.040
We hate subscriptions too.
00:22:21.040 --> 00:22:26.580
That's why our course bundle gives you full access to the entire library of courses for one fair price.
00:22:26.580 --> 00:22:27.700
That's right.
00:22:27.700 --> 00:22:33.640
With the course bundle, you save 70% off the full price of our courses and you own them all forever.
00:22:33.640 --> 00:22:39.760
That includes courses published at the time of the purchase, as well as courses released within about a year of the bundle.
00:22:40.000 --> 00:22:45.260
So stop subscribing and start learning at talkpython.fm/everything.
00:22:46.700 --> 00:22:47.260
All right.
00:22:47.260 --> 00:22:50.420
So the next chapter was functions and there's a lot of good stuff in there.
00:22:50.420 --> 00:22:57.760
One of them that I'm a fan of is the whole sort of concept of it's easier to ask for forgiveness than permission.
00:22:57.760 --> 00:22:58.860
Okay.
00:22:59.260 --> 00:23:05.560
And, you know, that sort of manifests in your recommendation of saying prefer exceptions rather than returning none from functions.
00:23:05.560 --> 00:23:06.000
Yeah.