Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Unions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next, I want to talk about something called unions. And unions allow you to express that a variable could be one type or another type.
0:09 So let's look at a couple here. We have a thing called UN1 and it's going to be the number 1 and UN2, which is going to be the word 2.
0:20 And for whatever reason, in this scenario, we want to explicitly say it could be a number or it could be a string, but nothing other than that.
0:29 All right. we express that is traditionally we would say typing.union of int and str. However,
0:39 in Python 3.10, they added some syntax that we just saw in the nullability section that
0:46 is a simpler version using the pipe symbol. So we could say int pipe str. And these two
0:54 things are defined have exactly the same type. It's just newer syntax. Remember, I told you
1:00 you need Python 3.10 as a minimum to do the examples in this course. Well, here's one
1:05 line that will only work in Python 3.10 or above. If you're using older versions of Python
1:10 3.5, or above, at least, you could use this. This syntax has generally been replaced with
1:17 this one and I'm pretty, pretty much on board with that. So let's put this as three here.
1:23 I'll put that as two so we can just do something we could print out
1:28 you one, you and one plus you and two. And if we run that you can see one plus two is three.
1:36 Excellent. In this case, we're using them as numbers, we saw that if it was a string and an
1:40 int it wouldn't work. Down here later we could say u n one equals one and u n two equals two.
1:49 Again, the type system says that's totally good. We run it again. Now we get one two.
1:55 Okay, use a space but we don't have one. But if we were trying to do something that didn't match
2:02 the int or string like you in one is now a list of numbers. We get this error and it
2:09 says, Oh, no, no, no, it could either be an int or a string, but it can't be some unrelated
2:16 thing like a list of integers. So we'll put this as error. Now, finally, when you look
2:23 at the auto complete and the things that you can do, you can do count, or you can do case
2:30 Case fold, these are string things, or you could do the real section or the imaginary section that are numerical type things.
2:38 Ask if it's a decimal, that's a string thing. But if you do something like case fold, which only happens to appear on strings and not
2:46 numbers, I believe in some of the linters, you're going to see that as an error.
2:50 It'll say the operation you're trying to apply to the union only exists on one of the elements.
2:56 So probably the safest way to think of this is you can do the things that happen to appear both on integers and on strings.
3:04 So you should think of taking a union here as something restrictive. It's less than an integer and it's less than a string.


Talk Python's Mastodon Michael Kennedy's Mastodon