Rock Solid Python with Python Typing Transcripts
Chapter: Typing Guidance, Patterns, and Advice
Lecture: Versions of Python

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One thing that can be a little frustrating with highly evolved and polished languages or libraries and so on is there's often multiple ways
0:11 to do the same thing. And nevermind the fact that the Zen of Python says there should be one and only way to do a thing and Python typing, not so much.
0:21 It came out and then we said, you know, it'd be a little bit better if we could do that. And what about this? It'd be nice if we could add that.
0:27 And so you'll see there are multiple ways in which you can write code based on what version of Python you wanna support.
0:34 In the older versions, we had to write from typing import list, import union, and even I still do because I like this better
0:43 as I just said, but you could still, you would say import optional. In modern Python, the latest versions, none of these are required to accomplish
0:52 what you're about to see. But what if you wanted to support something that's not 3.11, 3.12? Well, if we wanted to express, we have a list of things
1:01 and that those things are a bunch of strings. We could write capital L list str. Later on in Python, they said, that's really annoying
1:12 to have to import a thing called the capital L list. We don't have a thing called lowercase L list that is actually the type.
1:20 So list, the capital L version is like a weird stand in for the real one. That's weird, right? So, but the lowercase one at the beginning
1:31 didn't support this indexing in there 'cause what does that even mean, right? That was weird to it.
1:36 Later Python's type system was evolved to do this one where you don't require the list import. Now, the first one with the capital L list
1:44 will work in Python 3.5 or above, but the one with the lowercase L list will work with 3.9 and onward, right?
1:53 So you have to have a little bit newer version. At the time of the recording, 3.8 is the minimum supported version.
2:00 But you know, people have older code. I've had people reach out to me on YouTube videos or other things where I said,
2:08 Michael, you've written your code wrong. I've tried to run it, it just won't run. And it was this lowercase L list of some item.
2:18 And they said, look, it just keeps giving me this error. You just, you messed up and you need to fix it.
2:23 You're not really actually that good at Python. Okay, so that's one. One explanation for why this isn't running.
2:31 Another one could be you're running Python 3.7 and this code is written for 3.9. So you'll run in, maybe not on YouTube,
2:38 but you'll run into these issues running older code. And if you don't understand the type system, I'd be like, why, why does this not work?
2:45 You forgot to make it capital L and import it or something. But no, it's just about the versions. Here's another one, union.
2:51 So we could say union of string int, or more recently we could say string or int, the vertical bar, that's 3.5.
3:02 And this one is pretty new in 3.10 to use the vertical bar. We also could say a union of string or none. That's one possibility.
3:13 Or we could say string pipe none. Or you could go a little bit further back and choose my favorite way of saying this. An optional string.
3:22 So the union was a way to express this in 3.5. Also the optional was right there. And the 3.10 version allowed us to say string pipe none.
3:35 And while I think union, like the middle one, union of string int, I think string pipe int is really great, but string pipe none, I don't know,
3:46 it just doesn't communicate as clearly exactly what's happening. Although a lot of people prefer it, and if you do, you know, totally fine.
3:54 All right, so here's the, just gives you a sense. So you wanna think about what minimum Python version you're targeting as you write your code,
4:04 because there's some really simple things like string pipe int, or most commonly probably collection, lowercase collection class bracket some type
4:14 that you're gonna run into that you're gonna need a somewhat modern version of Python for.


Talk Python's Mastodon Michael Kennedy's Mastodon