WEBVTT

1
00:00:00.080 --> 00:00:03.160
<v Speaker 1>Welcome back to the Deep Dive. Today, we are focusing

2
00:00:03.240 --> 00:00:05.559
<v Speaker 1>on a tool that I suspect a huge portion of

3
00:00:05.599 --> 00:00:09.160
<v Speaker 1>our listeners use well every single day. Oh yeah, it's

4
00:00:09.240 --> 00:00:13.439
<v Speaker 1>the Swiss Army Knife of coding. It powers everything from

5
00:00:14.160 --> 00:00:18.320
<v Speaker 1>the little script that organizes your messy desktop all the

6
00:00:18.399 --> 00:00:21.920
<v Speaker 1>way to neural networks running the most advanced AI.

7
00:00:22.480 --> 00:00:25.120
<v Speaker 2>We are, of course talking about Python. It really is

8
00:00:25.160 --> 00:00:28.079
<v Speaker 2>the language of the moment. You see it everywhere web development,

9
00:00:28.280 --> 00:00:31.719
<v Speaker 2>data science, automation. I mean, it's just completely taken over.

10
00:00:31.839 --> 00:00:34.159
<v Speaker 1>But here's the catch, and this is specifically why we

11
00:00:34.280 --> 00:00:38.200
<v Speaker 1>chose today's source material. Because Python is so readable, so

12
00:00:38.399 --> 00:00:42.320
<v Speaker 1>beginner friendly. There's this massive trap people fall into. You

13
00:00:42.359 --> 00:00:44.799
<v Speaker 1>write a script, it runs, you get the output you wanted,

14
00:00:44.840 --> 00:00:47.520
<v Speaker 1>and you think, great, I know Python. I'm a Python developer.

15
00:00:47.640 --> 00:00:50.200
<v Speaker 2>Ah, the classic Dunning Krueger effect. It's so common with

16
00:00:50.240 --> 00:00:52.200
<v Speaker 2>Python because that barrier to entry is just so.

17
00:00:52.280 --> 00:00:55.280
<v Speaker 1>Low, exactly. And then you walk into that technical interview.

18
00:00:55.640 --> 00:00:58.759
<v Speaker 1>The interviewer leans back and asks, so tell me about

19
00:00:58.759 --> 00:01:00.200
<v Speaker 1>the global interpreter.

20
00:00:59.759 --> 00:01:04.439
<v Speaker 2>Lock, or how does Python actually handle garbage collection in

21
00:01:04.480 --> 00:01:05.359
<v Speaker 2>its private heap?

22
00:01:05.599 --> 00:01:08.640
<v Speaker 1>And suddenly you realize, I don't know Python. I just

23
00:01:08.680 --> 00:01:10.079
<v Speaker 1>know how to use Python syntax.

24
00:01:10.439 --> 00:01:13.799
<v Speaker 2>That is a very, very painful distinction to realize in

25
00:01:13.920 --> 00:01:17.560
<v Speaker 2>real time during an interview. It's that difference between scripting

26
00:01:17.599 --> 00:01:18.359
<v Speaker 2>and engineering.

27
00:01:18.760 --> 00:01:21.200
<v Speaker 1>So to save you from that pain, we're doing a

28
00:01:21.200 --> 00:01:26.359
<v Speaker 1>deep dive into Python interview questions by Swati Saxena. This

29
00:01:26.519 --> 00:01:30.840
<v Speaker 1>is a comprehensive guide from BPB Publications. Look, this isn't

30
00:01:30.879 --> 00:01:32.879
<v Speaker 1>just some list of gotcha questions to memorize.

31
00:01:32.959 --> 00:01:35.439
<v Speaker 2>No it's not. It's really a tour of the language's

32
00:01:35.439 --> 00:01:38.799
<v Speaker 2>internal architecture. It bridges that gap between I can make

33
00:01:38.799 --> 00:01:40.519
<v Speaker 2>it work and I understand how it works.

34
00:01:40.640 --> 00:01:40.840
<v Speaker 1>Right.

35
00:01:40.920 --> 00:01:42.680
<v Speaker 2>We're going to look at the memory model, the object

36
00:01:42.719 --> 00:01:46.840
<v Speaker 2>oriented pillars, even gy development, and some of those specific

37
00:01:47.000 --> 00:01:51.319
<v Speaker 2>performance bottlenecks like the dogpile effect that can crash your

38
00:01:51.359 --> 00:01:52.799
<v Speaker 2>system if you're not careful.

39
00:01:52.959 --> 00:01:55.000
<v Speaker 1>So if you're a developer looking to level up, or

40
00:01:55.079 --> 00:01:57.200
<v Speaker 1>just someone who wants to know what's actually happening when

41
00:01:57.239 --> 00:01:59.280
<v Speaker 1>you hit run, this one's for you. We'll start at

42
00:01:59.280 --> 00:02:02.519
<v Speaker 1>the very beginning, the origin story. Okay, because I have

43
00:02:02.560 --> 00:02:05.000
<v Speaker 1>to admit, for the longest time I looked at that

44
00:02:05.040 --> 00:02:10.240
<v Speaker 1>Python logo the two snakes, and I just assumed, hmmm, well, snakes.

45
00:02:10.039 --> 00:02:13.199
<v Speaker 2>It's the most reasonable assumption in the world. But as

46
00:02:13.280 --> 00:02:15.599
<v Speaker 2>Vaccina points out right at the start, it's wrong.

47
00:02:15.479 --> 00:02:19.520
<v Speaker 1>Totally wrong. Guido van Rossum, the creator, he released this

48
00:02:19.560 --> 00:02:22.199
<v Speaker 1>back in nineteen ninety one. He wasn't into reptiles. He

49
00:02:22.240 --> 00:02:23.639
<v Speaker 1>was into British comedy right.

50
00:02:23.680 --> 00:02:26.960
<v Speaker 2>He was reading scripts from Monty Python's Flying Circus while

51
00:02:26.960 --> 00:02:30.599
<v Speaker 2>he was developing it. He needed a name that was short, unique,

52
00:02:30.840 --> 00:02:32.199
<v Speaker 2>and a little bit mysterious.

53
00:02:32.280 --> 00:02:34.879
<v Speaker 1>So Python it was, which explains a lot about the

54
00:02:34.919 --> 00:02:35.840
<v Speaker 1>culture of the language.

55
00:02:35.840 --> 00:02:38.360
<v Speaker 2>Actually it does. It's supposed to be fun, although spam

56
00:02:38.439 --> 00:02:42.719
<v Speaker 2>and eggs, variables and tutorials instead of foo and bar exactly.

57
00:02:43.319 --> 00:02:46.479
<v Speaker 1>But let's get serious about the definitions. The book defines

58
00:02:46.560 --> 00:02:52.879
<v Speaker 1>Python as an interpreted high level object oriented language, almost

59
00:02:52.919 --> 00:02:56.680
<v Speaker 1>up on that first word interpreted, because for someone coming

60
00:02:56.680 --> 00:02:59.120
<v Speaker 1>from C or C plus plus a, this is a

61
00:02:59.159 --> 00:02:59.719
<v Speaker 1>major shift.

62
00:03:00.039 --> 00:03:02.439
<v Speaker 2>Really is in a compiled language L A C plus

63
00:03:02.560 --> 00:03:04.879
<v Speaker 2>plus A. You write your code, you run it through

64
00:03:04.879 --> 00:03:07.719
<v Speaker 2>a compiler, and it spits out machine code zeros and

65
00:03:07.800 --> 00:03:11.360
<v Speaker 2>ones that the processor crunches directly, super fast, incredibly fast.

66
00:03:11.400 --> 00:03:14.840
<v Speaker 2>But that compilation step takes time. Python doesn't really do that.

67
00:03:14.919 --> 00:03:17.479
<v Speaker 2>It adds a layer when you run a script. You're

68
00:03:17.520 --> 00:03:21.080
<v Speaker 2>actually running the interpreter, which reads your code line by

69
00:03:21.120 --> 00:03:22.680
<v Speaker 2>line and executes it.

70
00:03:23.120 --> 00:03:26.039
<v Speaker 1>Wait, I've seen these files of my project's folders. I

71
00:03:26.080 --> 00:03:28.800
<v Speaker 1>write a dot pi file, but sometimes a dot pi

72
00:03:28.879 --> 00:03:31.520
<v Speaker 1>file appears next to it, or in that pie cash folder.

73
00:03:31.919 --> 00:03:33.919
<v Speaker 1>I usually just ignore them or add them to my

74
00:03:34.000 --> 00:03:36.360
<v Speaker 1>dot j you'd ignore, but the author makes a point

75
00:03:36.360 --> 00:03:37.479
<v Speaker 1>that these are actually important.

76
00:03:37.639 --> 00:03:40.319
<v Speaker 2>They are and this is a nuance that catches people out.

77
00:03:40.960 --> 00:03:44.560
<v Speaker 2>Python is interpreted, but there is a compilation step happening

78
00:03:44.599 --> 00:03:47.520
<v Speaker 2>in the background. Okay, the interpreter doesn't just read your

79
00:03:47.520 --> 00:03:50.400
<v Speaker 2>english looking text and guess what to do. It first

80
00:03:50.439 --> 00:03:53.000
<v Speaker 2>compiles your dot PI file into something called bytecode.

81
00:03:53.120 --> 00:03:55.039
<v Speaker 1>Ah, and that's the dot PI file.

82
00:03:55.159 --> 00:03:56.520
<v Speaker 2>That is what's in the dot PI file.

83
00:03:56.560 --> 00:03:58.560
<v Speaker 1>It's like an intermediate language exactly.

84
00:03:58.599 --> 00:04:02.280
<v Speaker 2>It's a low level plus platform independent version of your code.

85
00:04:02.439 --> 00:04:06.719
<v Speaker 2>The Python Virtual Machine, the PVM, reads that ltecode. Think

86
00:04:06.759 --> 00:04:09.759
<v Speaker 2>of it like a chef prepping all the ingredients.

87
00:04:09.280 --> 00:04:11.560
<v Speaker 1>Before cooking, right the meuson plas.

88
00:04:11.800 --> 00:04:15.479
<v Speaker 2>Yes, the dot pike is the chopped vegetables. So next

89
00:04:15.479 --> 00:04:17.759
<v Speaker 2>time you run the program, if the code hasn't changed,

90
00:04:18.000 --> 00:04:20.480
<v Speaker 2>Python skips the prep and goes straight to cooking. It

91
00:04:20.560 --> 00:04:21.360
<v Speaker 2>just starts faster.

92
00:04:21.480 --> 00:04:24.600
<v Speaker 1>Okay, that makes sense, a hybrid approach for efficiency. Now,

93
00:04:24.639 --> 00:04:27.759
<v Speaker 1>speaking of writing the code, let's talk about the thing

94
00:04:27.800 --> 00:04:30.720
<v Speaker 1>that drives non Python developers absolutely crazy.

95
00:04:30.800 --> 00:04:32.600
<v Speaker 2>Oh yes, the white space.

96
00:04:32.319 --> 00:04:35.920
<v Speaker 1>War, indentation and other languages just you know, good manners.

97
00:04:36.160 --> 00:04:38.519
<v Speaker 1>You do it to your coworkers, don't hate you, but

98
00:04:38.639 --> 00:04:39.639
<v Speaker 1>the computer doesn't care.

99
00:04:39.879 --> 00:04:42.920
<v Speaker 2>Right in Java or C plus plus war, you use

100
00:04:43.000 --> 00:04:46.000
<v Speaker 2>curly braces to tell the computer this loop starts here

101
00:04:46.079 --> 00:04:49.000
<v Speaker 2>and it ends here. Python says, no, we don't do that.

102
00:04:49.040 --> 00:04:52.879
<v Speaker 1>Here, we do spaces in Python. Indentation is the syntax.

103
00:04:53.680 --> 00:04:55.920
<v Speaker 1>The book is very strict about this. It says it

104
00:04:55.959 --> 00:04:57.680
<v Speaker 1>specifies a block of code.

105
00:04:57.759 --> 00:05:00.839
<v Speaker 2>It forces you to write readable code. If you're spacing

106
00:05:00.920 --> 00:05:03.079
<v Speaker 2>is off. The interpreter doesn't just think it's ugly. It

107
00:05:03.120 --> 00:05:06.360
<v Speaker 2>throws an error. It literally won't understand where a loop ends.

108
00:05:06.720 --> 00:05:10.279
<v Speaker 2>It's a design choice that prioritizes human readability over almost

109
00:05:10.319 --> 00:05:10.879
<v Speaker 2>everything else.

110
00:05:11.000 --> 00:05:12.879
<v Speaker 1>Let's go deeper. Let's talk about this stuff. We can't

111
00:05:12.920 --> 00:05:16.480
<v Speaker 1>see memory management. This is where the interview questions start

112
00:05:16.480 --> 00:05:21.639
<v Speaker 1>getting really technical. The source mentions the Python private heap.

113
00:05:21.839 --> 00:05:24.480
<v Speaker 2>This is a critical concept in a language like C.

114
00:05:24.959 --> 00:05:28.040
<v Speaker 2>You're often manually grabbing memory from the system and then

115
00:05:28.120 --> 00:05:30.480
<v Speaker 2>releasing it. It's like renting a hotel room. You check in,

116
00:05:30.600 --> 00:05:31.720
<v Speaker 2>you check out, and if.

117
00:05:31.639 --> 00:05:33.480
<v Speaker 1>You forget to check out, you've got a memory leak.

118
00:05:33.519 --> 00:05:36.160
<v Speaker 2>You've got a memory leak. Python acts more like a

119
00:05:36.240 --> 00:05:38.879
<v Speaker 2>high end travel agent. It handles the bookings for you.

120
00:05:39.079 --> 00:05:42.079
<v Speaker 2>All your objects, your numbers, your lists, they all live

121
00:05:42.120 --> 00:05:45.839
<v Speaker 2>in this private heap. The key phrase Saxena uses is

122
00:05:45.879 --> 00:05:47.839
<v Speaker 2>that the programmer has no access to.

123
00:05:47.800 --> 00:05:50.800
<v Speaker 1>This, so it's a gated community, strictly gated.

124
00:05:50.920 --> 00:05:54.480
<v Speaker 2>The Python interpreter manages the whole thing. You create a variable,

125
00:05:54.720 --> 00:05:57.680
<v Speaker 2>it finds space, you stop using it, it cleans it up.

126
00:05:57.879 --> 00:06:00.879
<v Speaker 1>So how does it know when I'm done with something?

127
00:06:00.920 --> 00:06:03.279
<v Speaker 1>I mean, if I create a huge list of data

128
00:06:03.680 --> 00:06:05.600
<v Speaker 1>and then I just move on, how does Python know

129
00:06:05.720 --> 00:06:10.079
<v Speaker 1>it can delete that list? The book talks about reference counting, right.

130
00:06:09.920 --> 00:06:13.279
<v Speaker 2>This is the mechanism. Imagine every single object you create

131
00:06:13.360 --> 00:06:15.240
<v Speaker 2>has a little counter attached to it. So you say

132
00:06:15.439 --> 00:06:18.600
<v Speaker 2>x gos one, two, three, That list now has one reference.

133
00:06:18.680 --> 00:06:22.199
<v Speaker 2>The count is one. If you then say why equals x,

134
00:06:22.319 --> 00:06:25.240
<v Speaker 2>Now two variables point to it, the count is two.

135
00:06:25.560 --> 00:06:27.399
<v Speaker 1>It's tracking popularity exactly.

136
00:06:27.839 --> 00:06:30.600
<v Speaker 2>Now if you reassign X and say ex eques Hello,

137
00:06:31.199 --> 00:06:33.920
<v Speaker 2>X isn't pointing to the list anymore. The list's count

138
00:06:34.000 --> 00:06:36.680
<v Speaker 2>drops back to one. If you then delete, why, the

139
00:06:36.720 --> 00:06:37.319
<v Speaker 2>count hits.

140
00:06:37.240 --> 00:06:39.120
<v Speaker 1>Zero and zero means death.

141
00:06:39.480 --> 00:06:43.639
<v Speaker 2>Zero means the garbage collector swoops in immediately. It sees

142
00:06:43.720 --> 00:06:47.240
<v Speaker 2>the zero, reclaims that memory, and scrubs the spot for

143
00:06:47.279 --> 00:06:49.959
<v Speaker 2>the next object. It's very efficient for most cases because

144
00:06:49.959 --> 00:06:51.639
<v Speaker 2>it all just happens. You don't have to think about it.

145
00:06:51.759 --> 00:06:53.240
<v Speaker 1>I like that it's like a rum, but that just

146
00:06:53.360 --> 00:06:56.759
<v Speaker 1>runs constantly. Yeah, okay, let's move to data structures. This

147
00:06:56.839 --> 00:07:00.199
<v Speaker 1>is the bread and butter of coding interviews, specifically the

148
00:07:00.240 --> 00:07:02.120
<v Speaker 1>war between lists and tupples.

149
00:07:02.319 --> 00:07:05.160
<v Speaker 2>You will get asked this, guaranteed. It's probably the most

150
00:07:05.160 --> 00:07:07.199
<v Speaker 2>standard warm up technical question there is.

151
00:07:07.319 --> 00:07:09.639
<v Speaker 1>On the surface, they look almost the same. A list

152
00:07:09.720 --> 00:07:11.199
<v Speaker 1>is a bunch of things in order. A tuple is

153
00:07:11.199 --> 00:07:14.360
<v Speaker 1>a bunch of things in order. But the book emphasizes mutability.

154
00:07:14.560 --> 00:07:18.279
<v Speaker 2>That's the divider. A list defined with square brackets is mutable.

155
00:07:18.319 --> 00:07:20.199
<v Speaker 2>You can change it, you can add to it, delete

156
00:07:20.240 --> 00:07:22.480
<v Speaker 2>from it, swap items around. It's dynamic.

157
00:07:22.560 --> 00:07:23.439
<v Speaker 1>And the tupole.

158
00:07:23.360 --> 00:07:28.040
<v Speaker 2>Defined with corientheses and it is immutable sealed. Once you

159
00:07:28.079 --> 00:07:30.399
<v Speaker 2>create it, you cannot change a single thing about it.

160
00:07:30.480 --> 00:07:33.240
<v Speaker 1>Okay, here's where I play Dell's advocate, and I imagine

161
00:07:33.240 --> 00:07:35.879
<v Speaker 1>a listener asking this too. If a list can do

162
00:07:36.079 --> 00:07:40.079
<v Speaker 1>everything a tuple can do, plus be changed, why would

163
00:07:40.079 --> 00:07:41.959
<v Speaker 1>I ever use a tuple? It just sounds like a

164
00:07:42.000 --> 00:07:43.319
<v Speaker 1>list with fewer features.

165
00:07:43.480 --> 00:07:45.839
<v Speaker 2>That's a great question, and it's the follow up an

166
00:07:45.959 --> 00:07:48.720
<v Speaker 2>interviewer will ask to see if you understand system design.

167
00:07:49.000 --> 00:07:53.680
<v Speaker 2>There are two main reasons, safety and performance. Okay, safety wise,

168
00:07:53.959 --> 00:07:57.160
<v Speaker 2>think of map coordinates or server settings. You don't want

169
00:07:57.160 --> 00:07:59.720
<v Speaker 2>those to change by accident. Using a tuple is a

170
00:07:59.759 --> 00:08:01.639
<v Speaker 2>way of saying this is constant, don't touch.

171
00:08:01.839 --> 00:08:03.959
<v Speaker 1>It's a right protection sticker exactly.

172
00:08:04.319 --> 00:08:08.279
<v Speaker 2>And then there's performance. Because tuples are static, Python can

173
00:08:08.360 --> 00:08:11.759
<v Speaker 2>optimize them better. They use less memory, and they're faster

174
00:08:11.839 --> 00:08:14.720
<v Speaker 2>to iterate over than lists. So for a massive data set,

175
00:08:14.759 --> 00:08:18.279
<v Speaker 2>you're only reading from a tupol is the smarter, faster choice.

176
00:08:18.360 --> 00:08:20.879
<v Speaker 1>Okay, that clicks. Now, there's a function in the book

177
00:08:20.920 --> 00:08:22.639
<v Speaker 1>that I wanted to highlight because I actually use it

178
00:08:22.639 --> 00:08:24.199
<v Speaker 1>all the time and it just makes you look like

179
00:08:24.240 --> 00:08:25.279
<v Speaker 1>a pro DoD zip.

180
00:08:25.319 --> 00:08:28.639
<v Speaker 2>Zip is fantastic. It's one of those really pythonic tools

181
00:08:28.839 --> 00:08:31.319
<v Speaker 2>that saves you from writing a clunky four loop.

182
00:08:31.399 --> 00:08:34.559
<v Speaker 1>The book describes it as taking multiple iterables and mapping

183
00:08:34.559 --> 00:08:35.080
<v Speaker 1>them together.

184
00:08:35.279 --> 00:08:38.279
<v Speaker 2>The zipper analogy is perfect. Imagine you have the left

185
00:08:38.320 --> 00:08:42.080
<v Speaker 2>side of a zipper that's a list of names, Alice, Bob, Charlie.

186
00:08:42.320 --> 00:08:45.200
<v Speaker 2>The right side is a list of ages twenty five,

187
00:08:45.360 --> 00:08:47.759
<v Speaker 2>thirty thirty five, and you just zip them up. You

188
00:08:47.799 --> 00:08:51.159
<v Speaker 2>pull the slider and the teeth lock. Alice with twenty five,

189
00:08:51.559 --> 00:08:54.600
<v Speaker 2>Bob with thirty, Charlie with thirty five. It creates a

190
00:08:54.600 --> 00:08:55.960
<v Speaker 2>new object made of those bears.

191
00:08:56.080 --> 00:08:58.399
<v Speaker 1>But what if my lists are messy? What if I

192
00:08:58.399 --> 00:09:01.919
<v Speaker 1>have five names but only three eight ages? Does it crash?

193
00:09:02.080 --> 00:09:04.799
<v Speaker 2>It doesn't crash, but it does something very specific. You

194
00:09:04.840 --> 00:09:07.240
<v Speaker 2>need to know. The zipper stops at the shorder list.

195
00:09:07.360 --> 00:09:09.960
<v Speaker 2>Those extra two names are just left hanging. They get ignored.

196
00:09:10.120 --> 00:09:14.000
<v Speaker 1>That's a good gotcha to remember. It truncates one more

197
00:09:14.039 --> 00:09:17.279
<v Speaker 1>structure before we move on sets. The book mentioned set

198
00:09:17.360 --> 00:09:18.159
<v Speaker 1>and frozen set.

199
00:09:18.399 --> 00:09:20.679
<v Speaker 2>A set is just like the mathematical concept. It's an

200
00:09:20.720 --> 00:09:24.360
<v Speaker 2>unordered collection, and the key thing is no duplicates. If

201
00:09:24.399 --> 00:09:26.399
<v Speaker 2>you have a list with ten apples and you convert

202
00:09:26.440 --> 00:09:28.440
<v Speaker 2>it to a set, you just get one apple. It's

203
00:09:28.480 --> 00:09:30.879
<v Speaker 2>the fastest way to get unique items from data.

204
00:09:30.919 --> 00:09:31.799
<v Speaker 1>And a frozen set just.

205
00:09:31.799 --> 00:09:34.039
<v Speaker 2>Apply the logical use for tupls. A frozen set is

206
00:09:34.039 --> 00:09:37.279
<v Speaker 2>an immutable set. You create it and then you can't

207
00:09:37.279 --> 00:09:40.240
<v Speaker 2>add or remove anything. It's frozen. This is useful because

208
00:09:40.320 --> 00:09:43.320
<v Speaker 2>only immutable objects can be used as keys in a dictionary.

209
00:09:43.399 --> 00:09:47.080
<v Speaker 1>Ah I see Okay. Moving into object oriented programming, the

210
00:09:47.159 --> 00:09:51.080
<v Speaker 1>source makes a bold claim. In Python, everything is an object.

211
00:09:51.200 --> 00:09:54.039
<v Speaker 2>It really means everything. In some languages you have primitive

212
00:09:54.120 --> 00:09:57.639
<v Speaker 2>types like integers that are just raw data. In Python,

213
00:09:57.879 --> 00:10:01.320
<v Speaker 2>the number five is an object. It has methods. A

214
00:10:01.360 --> 00:10:04.200
<v Speaker 2>string is an object. Even a function is an object.

215
00:10:04.240 --> 00:10:06.080
<v Speaker 1>That is a real head scratcher if you're not used

216
00:10:06.080 --> 00:10:10.200
<v Speaker 1>to it. And with objects come classes. The book details

217
00:10:10.200 --> 00:10:13.519
<v Speaker 1>the life cycle methods specifically in it and Dela in

218
00:10:13.559 --> 00:10:14.120
<v Speaker 1>No Right.

219
00:10:14.600 --> 00:10:17.399
<v Speaker 2>In it is the constructor. It's the first thing that

220
00:10:17.480 --> 00:10:19.679
<v Speaker 2>runs when you create an instance of a class. It

221
00:10:19.720 --> 00:10:22.440
<v Speaker 2>sets the stage, gives the object its initial state, and

222
00:10:22.519 --> 00:10:25.039
<v Speaker 2>dell the destructor. It runs when the object is all

223
00:10:25.120 --> 00:10:25.759
<v Speaker 2>to be destroyed.

224
00:10:25.919 --> 00:10:28.080
<v Speaker 1>But the expert note in the book suggests we shouldn't

225
00:10:28.080 --> 00:10:29.159
<v Speaker 1>really rely on dell it.

226
00:10:29.240 --> 00:10:32.000
<v Speaker 2>Why is that because of that automatic garbage collection we

227
00:10:32.039 --> 00:10:34.759
<v Speaker 2>talked about, you don't always know exactly when the garbage

228
00:10:34.799 --> 00:10:36.840
<v Speaker 2>collector is going to run. So if you put critical

229
00:10:36.879 --> 00:10:41.480
<v Speaker 2>logic in del like close this database connection, it might

230
00:10:41.519 --> 00:10:44.200
<v Speaker 2>not run when you expect it to. Not reliable, not

231
00:10:44.279 --> 00:10:47.559
<v Speaker 2>reliable enough. It's better to handle that kind of cleanup explicitly.

232
00:10:47.799 --> 00:10:50.639
<v Speaker 1>That's a pro tip. Don't trust the garbage man to

233
00:10:50.639 --> 00:10:55.039
<v Speaker 1>lock your front door. Okay. Inheritance, the book talks about

234
00:10:55.159 --> 00:10:56.399
<v Speaker 1>using the super command.

235
00:10:56.759 --> 00:11:00.200
<v Speaker 2>Super is how you access the parent class. So if

236
00:11:00.200 --> 00:11:02.480
<v Speaker 2>you have a generic class called shape and you build

237
00:11:02.480 --> 00:11:05.759
<v Speaker 2>a specific class called square, you might want to reuse

238
00:11:05.799 --> 00:11:08.960
<v Speaker 2>logic that already exists in shape. Use super to call

239
00:11:09.039 --> 00:11:11.279
<v Speaker 2>up to the parent so you're not rewriting cut exactly.

240
00:11:11.320 --> 00:11:14.200
<v Speaker 2>It keeps things dr don't repeat yourself.

241
00:11:14.240 --> 00:11:16.440
<v Speaker 1>Now here's a question that I think I have failed

242
00:11:16.480 --> 00:11:21.519
<v Speaker 1>in a mock interview before. The difference between is and agle.

243
00:11:22.480 --> 00:11:24.840
<v Speaker 1>They both sound like they're checking if things are equal.

244
00:11:25.000 --> 00:11:29.240
<v Speaker 2>This is the ultimate trap. In English, is and equals

245
00:11:29.279 --> 00:11:32.639
<v Speaker 2>are synonyms. In Python, they are totally different operations.

246
00:11:32.679 --> 00:11:34.519
<v Speaker 1>Break it down for me, Okay.

247
00:11:34.440 --> 00:11:38.200
<v Speaker 2>Checks for value, it asks does the thing in box

248
00:11:38.279 --> 00:11:40.960
<v Speaker 2>A look exactly like the thing in box B. If

249
00:11:41.000 --> 00:11:43.919
<v Speaker 2>you have two lists both containing one, two, three, will

250
00:11:43.919 --> 00:11:45.080
<v Speaker 2>say yes, these are equal.

251
00:11:45.120 --> 00:11:45.840
<v Speaker 1>Okay, that makes sense.

252
00:11:45.879 --> 00:11:48.679
<v Speaker 2>It's checks for identity. It asks is this actually the

253
00:11:48.679 --> 00:11:51.039
<v Speaker 2>same box. It's checking the memory address.

254
00:11:51.159 --> 00:11:54.440
<v Speaker 1>So going back to those two lists with one, two three, right.

255
00:11:54.320 --> 00:11:56.720
<v Speaker 2>So list A is true, but list A is List

256
00:11:56.759 --> 00:11:59.559
<v Speaker 2>B is false. Ah, they're twins, but they are two

257
00:11:59.600 --> 00:12:01.559
<v Speaker 2>different people living in two different houses.

258
00:12:01.639 --> 00:12:03.919
<v Speaker 1>I love that analogy. Twins are equal, but they are

259
00:12:03.960 --> 00:12:04.279
<v Speaker 1>not the.

260
00:12:04.200 --> 00:12:07.039
<v Speaker 2>Same person precisely. Unless you do list B list A,

261
00:12:07.519 --> 00:12:10.440
<v Speaker 2>then both variables point to the same object. Then is

262
00:12:10.519 --> 00:12:11.399
<v Speaker 2>returns true.

263
00:12:11.679 --> 00:12:16.360
<v Speaker 1>Got it crucial? Okay, let's pivot. A huge chunk of

264
00:12:16.399 --> 00:12:19.000
<v Speaker 1>this book is dedicated to something we can't really show,

265
00:12:19.080 --> 00:12:22.080
<v Speaker 1>but we can visualize GUI programming with.

266
00:12:22.120 --> 00:12:25.440
<v Speaker 2>Kinter right Tinker is the standard GUI toolkit. It comes

267
00:12:25.519 --> 00:12:27.799
<v Speaker 2>bundled with Python, which is why it's so popular for

268
00:12:27.879 --> 00:12:28.919
<v Speaker 2>quick desktop apps.

269
00:12:29.000 --> 00:12:31.919
<v Speaker 1>The book walks through building windows and adding widgets. We

270
00:12:31.960 --> 00:12:35.639
<v Speaker 1>have buttons labels, but it makes a distinction between radio

271
00:12:35.679 --> 00:12:37.200
<v Speaker 1>buttons and check buttons.

272
00:12:37.519 --> 00:12:40.759
<v Speaker 2>Think of filling out a form. A radio button is exclusive,

273
00:12:41.360 --> 00:12:43.600
<v Speaker 2>like a car radio. You can only pick one station

274
00:12:44.000 --> 00:12:47.399
<v Speaker 2>if you click am FMD selects and the check button

275
00:12:47.519 --> 00:12:51.159
<v Speaker 2>that's select all that apply like pizza toppings. You can

276
00:12:51.200 --> 00:12:53.159
<v Speaker 2>have pepperonia and d mushrooms.

277
00:12:53.200 --> 00:12:56.639
<v Speaker 1>Now place these on screen. The source gets into geometry

278
00:12:56.679 --> 00:13:00.440
<v Speaker 1>management and list this very specific string format three hundred

279
00:13:00.440 --> 00:13:02.559
<v Speaker 1>by three hundred plus two hundred plus two hundred.

280
00:13:02.639 --> 00:13:04.840
<v Speaker 2>It looks like a math equation, but it's a coordinate map.

281
00:13:04.879 --> 00:13:07.440
<v Speaker 2>The first part three hundred by three hundred is within height,

282
00:13:07.759 --> 00:13:10.360
<v Speaker 2>the second part plus two hundred plus two hundred is

283
00:13:10.360 --> 00:13:13.519
<v Speaker 2>the position two hundred pixels from the left to the screen,

284
00:13:13.720 --> 00:13:14.799
<v Speaker 2>two hundred from the top.

285
00:13:14.919 --> 00:13:18.000
<v Speaker 1>And then there's resizable zero, which just locks the window.

286
00:13:18.080 --> 00:13:21.000
<v Speaker 2>The zero's act is false, so you're saying no resizing

287
00:13:21.000 --> 00:13:23.279
<v Speaker 2>the wits no resizing the height. The user can't drag

288
00:13:23.320 --> 00:13:23.799
<v Speaker 2>the corners.

289
00:13:23.840 --> 00:13:26.039
<v Speaker 1>Now, making the buttons actually do something, you use the

290
00:13:26.080 --> 00:13:26.879
<v Speaker 1>command parameter.

291
00:13:27.039 --> 00:13:29.600
<v Speaker 2>Right, you link the button click to a function command

292
00:13:29.600 --> 00:13:30.000
<v Speaker 2>my function.

293
00:13:30.120 --> 00:13:33.000
<v Speaker 1>But here's the problem the book solves. Suppose my function

294
00:13:33.080 --> 00:13:36.759
<v Speaker 1>needs arguments like add five to ten. If I write

295
00:13:36.799 --> 00:13:40.000
<v Speaker 1>command add five to ten in the code, Python runs

296
00:13:40.000 --> 00:13:42.159
<v Speaker 1>that function immediately when the program starts.

297
00:13:42.440 --> 00:13:46.120
<v Speaker 2>Yes, exactly, because you put the parentheses, Python executes it right.

298
00:13:46.159 --> 00:13:47.720
<v Speaker 2>Then it doesn't wait for the click.

299
00:13:47.879 --> 00:13:50.759
<v Speaker 1>So how do we fix that. The book suggests something

300
00:13:50.799 --> 00:13:51.919
<v Speaker 1>called partial.

301
00:13:51.879 --> 00:13:54.919
<v Speaker 2>Yes from the Funk tools library. This is a life saver.

302
00:13:55.320 --> 00:13:58.879
<v Speaker 2>Partial basically freezes the function along with its arguments. It

303
00:13:58.919 --> 00:14:03.000
<v Speaker 2>wraps add five ten in a package and says, don't

304
00:14:03.039 --> 00:14:05.159
<v Speaker 2>open this until the button is clicked.

305
00:14:05.279 --> 00:14:07.960
<v Speaker 1>So functals dot partial is the secret sauce for passing

306
00:14:08.120 --> 00:14:09.200
<v Speaker 1>arguments in callbacks.

307
00:14:09.240 --> 00:14:12.960
<v Speaker 2>It's pretty much essential for any complex gy programming.

308
00:14:13.039 --> 00:14:16.960
<v Speaker 1>Okay, we're entering the final stretch advanced concepts. This is

309
00:14:16.960 --> 00:14:19.600
<v Speaker 1>where the seniors get separated from the juniors. Let's talk

310
00:14:19.600 --> 00:14:20.320
<v Speaker 1>about speed.

311
00:14:20.480 --> 00:14:21.799
<v Speaker 2>Everyone wants faster code.

312
00:14:21.919 --> 00:14:24.519
<v Speaker 1>So logically, if I have a powerful computer with eight cores,

313
00:14:24.639 --> 00:14:26.879
<v Speaker 1>as you just use multi threading right split the work up.

314
00:14:26.960 --> 00:14:29.399
<v Speaker 2>You would think so that works beautifully in Java or

315
00:14:29.440 --> 00:14:32.480
<v Speaker 2>C plus plus mating, But the source drops a major

316
00:14:32.519 --> 00:14:36.120
<v Speaker 2>reality check here. For Python, it introduces the GIL. The

317
00:14:36.159 --> 00:14:37.360
<v Speaker 2>Global interpreter lock.

318
00:14:37.639 --> 00:14:38.480
<v Speaker 1>Sounds ominous.

319
00:14:38.840 --> 00:14:42.240
<v Speaker 2>It is if you're trying for parallel processing. The GIL

320
00:14:42.679 --> 00:14:45.759
<v Speaker 2>is a lock that prevents multiple native threads from executing

321
00:14:45.799 --> 00:14:47.519
<v Speaker 2>Python bytecodes at the same time.

322
00:14:47.759 --> 00:14:50.120
<v Speaker 1>Wait, so even if I have multiple threads, they aren't

323
00:14:50.159 --> 00:14:51.120
<v Speaker 1>running at the same time.

324
00:14:51.279 --> 00:14:54.960
<v Speaker 2>Now the GIL forces them to take turns. It happens

325
00:14:54.960 --> 00:14:57.840
<v Speaker 2>so fast that it looks parallel, but the CPU is

326
00:14:57.919 --> 00:15:00.399
<v Speaker 2>only ever doing one thing at a time. It's a

327
00:15:00.440 --> 00:15:01.559
<v Speaker 2>single lane highway.

328
00:15:02.200 --> 00:15:06.519
<v Speaker 1>So for CPU heavy tasks like crunching massive numbers, threading

329
00:15:06.759 --> 00:15:08.120
<v Speaker 1>might actually make things slower.

330
00:15:08.360 --> 00:15:10.960
<v Speaker 2>Yes, because of the overhead of switching back and forth

331
00:15:10.960 --> 00:15:13.759
<v Speaker 2>between the threads, you spend more time managing them than

332
00:15:13.840 --> 00:15:15.039
<v Speaker 2>doing actual work.

333
00:15:15.159 --> 00:15:18.399
<v Speaker 1>That is a massive gotcha. Yeah, So when should we

334
00:15:18.519 --> 00:15:19.720
<v Speaker 1>use threads in Python?

335
00:15:20.000 --> 00:15:22.919
<v Speaker 2>For io tasks waiting for a web page to load,

336
00:15:23.200 --> 00:15:26.320
<v Speaker 2>writing to a file anytime, the program is just waiting.

337
00:15:26.399 --> 00:15:29.000
<v Speaker 2>In those cases, the GIL releases the lock while it

338
00:15:29.039 --> 00:15:32.159
<v Speaker 2>waits so other threads can run. But for raw computation,

339
00:15:32.639 --> 00:15:34.039
<v Speaker 2>the GIL is a bottleneck.

340
00:15:34.200 --> 00:15:36.159
<v Speaker 1>Speaking of bottlenecks, there's a term in the book I

341
00:15:36.200 --> 00:15:37.559
<v Speaker 1>Love the dogpile effect.

342
00:15:37.840 --> 00:15:39.360
<v Speaker 2>It's a vivid image, isn't it?

343
00:15:39.360 --> 00:15:40.159
<v Speaker 1>It is? What is it?

344
00:15:40.320 --> 00:15:42.480
<v Speaker 2>So? Imagine you have a website that shows the weather.

345
00:15:42.639 --> 00:15:45.799
<v Speaker 2>It's a complex calculation that takes say five seconds to run.

346
00:15:45.960 --> 00:15:47.639
<v Speaker 2>You don't want to run it for every user, so

347
00:15:47.679 --> 00:15:49.519
<v Speaker 2>you calculate at once and save it in.

348
00:15:49.480 --> 00:15:53.080
<v Speaker 1>The cash smart Everyone just reads the saved copy, but.

349
00:15:53.080 --> 00:15:57.440
<v Speaker 2>That cash expires. Now, imagine at the exact second it expires.

350
00:15:57.559 --> 00:15:59.759
<v Speaker 2>One hundred users hit the website.

351
00:16:00.000 --> 00:16:01.720
<v Speaker 1>They all see the cash as empty, and.

352
00:16:01.639 --> 00:16:03.919
<v Speaker 2>They all try to run that five second calculation at

353
00:16:03.960 --> 00:16:06.799
<v Speaker 2>the same time, and the server just melts. That's the dogpile,

354
00:16:06.919 --> 00:16:09.879
<v Speaker 2>everyone jumping on the server at once. The CPU spikes

355
00:16:09.879 --> 00:16:12.519
<v Speaker 2>to one hundred percent and the site goes down.

356
00:16:12.840 --> 00:16:16.679
<v Speaker 1>The source suggests using a semaphore lock to fix this.

357
00:16:17.039 --> 00:16:17.840
<v Speaker 1>How does that work.

358
00:16:18.000 --> 00:16:20.399
<v Speaker 2>It's like a bathroom key at a gas station. The

359
00:16:20.440 --> 00:16:23.480
<v Speaker 2>first process sees the cash is empty, grabs the key,

360
00:16:23.679 --> 00:16:26.720
<v Speaker 2>locks the door, and does the calculation. The other ninety

361
00:16:26.759 --> 00:16:29.240
<v Speaker 2>nine processes see the door is locked, and they just wait.

362
00:16:29.600 --> 00:16:31.360
<v Speaker 1>They don't try to calculate it themselves.

363
00:16:31.440 --> 00:16:34.279
<v Speaker 2>Nope, they queue up. When the first one finishes, it

364
00:16:34.360 --> 00:16:37.279
<v Speaker 2>refills the cash, unlocks the door, and everyone else gets

365
00:16:37.279 --> 00:16:38.399
<v Speaker 2>the new data instantly.

366
00:16:38.519 --> 00:16:41.360
<v Speaker 1>That is brilliant. It's not just coding, it's architecture.

367
00:16:41.440 --> 00:16:42.919
<v Speaker 2>That's what senior interviews are about.

368
00:16:42.919 --> 00:16:45.399
<v Speaker 1>And we have just a minute left. Two quick definitions

369
00:16:45.960 --> 00:16:47.039
<v Speaker 1>Lambda versus.

370
00:16:46.879 --> 00:16:51.480
<v Speaker 2>Deaf simple distinction. Def creates a standard function with a name.

371
00:16:51.879 --> 00:16:55.440
<v Speaker 2>It can have multiple lines, loops whatever. Lambda is an

372
00:16:55.440 --> 00:16:58.679
<v Speaker 2>anonymous function, no name, and it can only evaluate a

373
00:16:58.720 --> 00:17:01.960
<v Speaker 2>single expression. It's for quick throwaway logic.

374
00:17:01.879 --> 00:17:04.680
<v Speaker 1>And finally Chathon with a Jay.

375
00:17:04.880 --> 00:17:08.759
<v Speaker 2>It's Python implemented in Java. It compiles Python code into

376
00:17:08.880 --> 00:17:10.680
<v Speaker 2>Java byte code so it can run on the Java

377
00:17:10.759 --> 00:17:14.039
<v Speaker 2>virtual machine. Why would you ever do that interoperability if

378
00:17:14.079 --> 00:17:16.599
<v Speaker 2>you're in a big corporate environment running on a javastack

379
00:17:17.000 --> 00:17:19.759
<v Speaker 2>gython lets you write clean Python scripts that can natively

380
00:17:19.880 --> 00:17:23.559
<v Speaker 2>use Java classes. It's a bridge between worlds. Wow, a

381
00:17:23.680 --> 00:17:24.559
<v Speaker 2>very useful one.

382
00:17:24.599 --> 00:17:26.799
<v Speaker 1>We have covered a massive amount of ground today. We

383
00:17:26.839 --> 00:17:29.160
<v Speaker 1>went from Monty Python to the private heap, learned that

384
00:17:29.200 --> 00:17:32.039
<v Speaker 1>tupples are the safer twins of lists, and learned why

385
00:17:32.079 --> 00:17:34.279
<v Speaker 1>the GIL keeps Python single minded.

386
00:17:34.440 --> 00:17:36.960
<v Speaker 2>It really shows that knowing the syntax is just step one.

387
00:17:37.400 --> 00:17:41.680
<v Speaker 2>Mastering Python means understanding the memory, the interpreter and its limitations.

388
00:17:41.960 --> 00:17:44.559
<v Speaker 1>And that's the takeaway for you, whether you are prepping

389
00:17:44.599 --> 00:17:47.240
<v Speaker 1>for an interview or just building your next app. Don't

390
00:17:47.279 --> 00:17:50.799
<v Speaker 1>just ask does it work? Ask where's this stored in memory?

391
00:17:51.200 --> 00:17:53.480
<v Speaker 1>And what happens if one hundred people try to do

392
00:17:53.519 --> 00:17:54.000
<v Speaker 1>this at once?

393
00:17:54.240 --> 00:17:56.519
<v Speaker 2>That curiosity is what makes a senior engineer.

394
00:17:56.720 --> 00:17:58.640
<v Speaker 1>And I want to leave you with one final thought

395
00:17:58.759 --> 00:18:01.440
<v Speaker 1>based on that dog pile of We live in a

396
00:18:01.480 --> 00:18:04.759
<v Speaker 1>world that demands instant data. We treat our caches in

397
00:18:04.839 --> 00:18:08.759
<v Speaker 1>databases like magic. But as we learn today, when the

398
00:18:08.799 --> 00:18:12.839
<v Speaker 1>magic stops even for a second, the dogpile happens. So

399
00:18:12.920 --> 00:18:15.759
<v Speaker 1>ask yourself, is your code ready for the moment the

400
00:18:15.799 --> 00:18:16.640
<v Speaker 1>cash expires?

401
00:18:16.839 --> 00:18:18.400
<v Speaker 2>That is the real stress test.

402
00:18:18.480 --> 00:18:20.519
<v Speaker 1>Thanks for diving deep with us today. Go check out

403
00:18:20.519 --> 00:18:23.240
<v Speaker 1>Swatty Saksina's book if you want to drill these questions further.

404
00:18:23.480 --> 00:18:25.440
<v Speaker 1>Until next time code responsibly

405
00:18:25.519 --> 00:18:25.960
<v Speaker 2>See you then,
