Rock Solid Python with Python Typing Transcripts
Chapter: Typing in Python
Lecture: Nullable Types

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's look at nullability. Even though the word doesn't appear in Python, I think null is kind of the standard idea of a reference thing,
0:10 reference variable pointing at nothing across the languages, right? Swift has nil, Python has none, most languages have null, but pointer at nothing.
0:21 Remember that when we said that spectrum of languages, I put things like C# and Swift to the more strict side,
0:30 even then C++ because partially because of its their nullability or their ability to check and make sure that certain data types can never be set to none,
0:42 even if that would be potentially a meaningful value in the implementation like zero might be the value for null for a pointer, but
0:50 you still can't assign it to an integer if that integer can't be nullable.
0:54 In Python, in its type system is exactly like this. It's super strict. It's more strict than
1:00 many of the languages that have concepts of reference types or pointers. So let's look at
1:05 that as well. When I have a variable called z, and it's going to be an int. And let's set that to
1:12 our favorite number 42. So what if we were to try to set z to be none. So right now z is 42. But
1:20 sometimes maybe we have no value for it. So we're going to set it to be none. And remember, there's
1:24 there's always this weirdo, you reassigned it error. So let's get that out and see the real error.
1:31 And the error is, we expected a type int and we got none. That is because not just integers, but every single thing in Python,
1:40 unless in its type system, unless you say explicitly, it can be set to none. It cannot, it will not be allowed. So here you might say, well, okay,
1:51 I'm familiar with languages like C# they have value types and reference types, and it doesn't make sense for a value type.
1:57 That's not what's happening here. We could have another z2, which is a string, which is equal to never nothing. We tried to set z2 equal to none.
2:08 And we printed it out. We still get exactly the same error. So even things that are reference types are allocated on the heap, they cannot be set
2:18 to none in the type system unless you explicitly say so. So again, this is an error. So what are we supposed to do about it?
2:26 Well, we declare the type to be possibly nullable or in Python's parlance, we say it's optional. The value could be there, but it doesn't have to be.
2:37 So we'll have Z3, which let's keep going the int example, I suppose, which instead of saying in an int, which is 43, we want to say this is optional.
2:48 And so this is where we have to use the typing namespace. So far we've gotten away mostly with just using native types to express the type system here,
2:57 but there's a whole typing module or a namespace which we can import. And in here we have optional.
3:04 Now there's different ways in which we can say this. Here's my favorite way. We'll talk more about the different ways in which it can be expressed,
3:13 But I like to say it this way because I find it very explicit. Although I typically don't import the typing namespace and say typing.optional.
3:21 I'm just doing that to make it real clear. More likely what I would write would be this and just import. From typing import optional.
3:33 And I would just write it like this. Okay. But to be super clear so you know, oh, it's coming from that namespace. Optional of this.
3:41 And down here now, we can go through and say Z3 is equal to none. That's fine. I got to keep printing it out or something silly, right?
3:55 To get those errors to go away. We can say Z3 is none or we could say Z3 is a new string. Sorry, a new number. 44. That's totally fine.
4:07 But what we can't do still is we can't set it to be equal to ABC. Even ignoring that little print error, it says it's an optional integer.
4:18 So we can get either integer or none. And you can see one of the alternate popular ways to express that would be we could say
4:27 same thing as Z4 and pipe none. So this is another option here on how to express. These two things are defined to have the same type.
4:39 You can even see that PyCharm interpreted the optional event as this int pipe none. This is a 310 syntax. Talk more about it later.
4:48 So depending on your background, this view of nullability may surprise you.
4:51 If you've come from a language like C++ where sure they have this concept of pointers and
4:56 even value types, there's no pointers that cannot be set to none unless you explicitly
5:03 allow them. However, this does appear in Swift as well, some of the other languages, but
5:09 Python's types, you by default are not allowed to set them to none. They must have a value
5:15 unless they are explicitly set to be optional of that type. Takes some getting used to, but I think I really like it.


Talk Python's Mastodon Michael Kennedy's Mastodon