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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Are you a minimalist or is your space kind of cluttered with a bunch of knickknacks and collectibles or other stuff just laying around?
0:11 Well, I personally, at least in theory, would love to be a minimalist, although I'm not really sure how practical that is,
0:18 especially with the family and kids and pets and stuff everywhere. But in Python typing, it is good to have this minimalist attitude towards code,
0:30 regardless of what the world looks like outside. Let's look at an example. So here we have a function, so creative, called func,
0:37 and it takes some things, and we've expressed the type of these things. Awesome. It's a list of integers. So we're gonna do fun stuff with this list,
0:46 like for T in things, we're gonna work with each T, and because we've expressed that the list contains integers, T will be an int in the type system,
0:56 and further checking and inference and stuff will happen. It's gonna be fantastic. So how would we use this? Let's explore some ways.
1:05 If we have some numbers, here we have the first five Fibonacci numbers as a list. So the bracket means it's a proper list. We call the function.
1:15 Perfect. The type system says, ""You know what, Michael? You're a great programmer. Everything is just like it's supposed to be. Okay.
1:24 Well, what other ways could we use this function that seem reasonable? We just wanna collection and loop over them.
1:29 Well, what if our numbers were actually in a tuple? Here we have the prime numbers, plus one, plus the number one,
1:39 here that we might want to funk on, right? We're gonna pass it over. Well, if you just look at what's on the screen so far,
1:48 will this work for T in things? Sure. I'm sure it'll work just fine. However, the type system says, No, no, no, this is not gonna work.
1:57 You said we got a list. You're giving us a tuple. Those are not the same, not even close. Sure, there are collections that are flat,
2:06 collections of single dimensional things. And sure, you have numbers in your tuple, but for example, you can't sort a tuple,
2:14 you can't put new items or take items out, they're immutable, et cetera. So they're not the same. And the type system is right to say,
2:24 You expected a list, and you gave us something not a list. But looking above, would that work for T in this tuple? Absolutely, we'll just go,
2:34 here's one, here's two, here's three, here's five, here's seven, fine. Similarly, what if we get more creative and have a generator expression,
2:41 where the generator on demand as you process over them will generate the square of whatever's in numbers. So one, one, four, nine, so on.
2:51 Now, also a generator is even more different than a tuple from a list, right? These are really, really different things.
3:01 You can't even index like bracket three, can't even do that to the generator. So sure, the type system is right,
3:07 but is that really what we need for our func? No, what if we were just said, All I need to be able to do is to use this in a for in loop.
3:17 After that, I don't care what's in there. It could be a set of integers, it could be a list of integers, it could be a tuple of integers, right?
3:25 So we wanna make sure that when we're putting types in our code, we're not just using, Oh, well, what's top of the mind?
3:32 Oh, I'm gonna use a list here, so list. Fine, if it needs to be a list, but if it doesn't, if it's more general, then think about our friend, the duck,
3:40 think about duck typing, and if it can behave in this way, it is one. So one way to express that without say protocols, which is totally different,
3:49 is to just use a more generic, a more general type of thing. So we can say from typing import iterable, and then we have an iterable of int,
3:59 and that just means, well, if it's anything I can put into a for in loop, that's all I need, right? In this case, all three examples would be fine,
4:08 the list, the tuple, and the generator.


Talk Python's Mastodon Michael Kennedy's Mastodon