Rock Solid Python with Python Typing Transcripts
Chapter: Typing Guidance, Patterns, and Advice
Lecture: Minimalism Code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So here we're in a new chapter, so chapter seven, type guidance and patterns. I put a couple of starter pieces of code here
0:09 for us to look at, and again, I marked directory as a sources root so that it can find each other. This one doesn't need it, but the next ones do.
0:18 All right, so here's our example. You can see we've got our function, we have numbers, we have numbers as tuples,
0:23 we have numbers as a generator expression, and it just says, look, we're gonna loop over those and we're gonna print them out
0:28 and we're gonna do math like square them. So even though there's an error here, it says, whoa, whoa, whoa, we got a tuple of, instead of a list of int,
0:39 or we got a generator instead of list of int. Let's run this and see what happens. At the top here, we print out the type.
0:52 So list tuple generator, sure enough, but look, it works just fine. Not the same output 'cause we're not giving it
0:59 the same numbers, but absolutely it works just fine. So we can come up, let's say, I'll call this func cluttered. I'll make a copy real quick.
1:12 It's this one. It's not minimalist, right? So over here, we can just say, we're gonna say iterable, and we've gotta import that from typing of int.
1:24 And notice that error goes away, that error goes away, because guess what? Just like the list, we can use those. There you go.
1:32 So runs the same, of course, but more importantly, the errors go away because we were more minimalist.
1:37 We just said, look, I don't really care what it is. If I can forward it and it contains integers, we're good to go. And that's true for all of them.


Talk Python's Mastodon Michael Kennedy's Mastodon