Rock Solid Python with Python Typing Transcripts
Chapter: Static vs. Dynamic Languages
Lecture: Spectrum of Type Strictness

Login or purchase this course to watch this video and the rest of the course contents.
0:00 When we think about different programming languages and especially their type systems,
0:05 the dynamically typed ones, strictly or strongly typed ones, and maybe things that live somewhere
0:11 in the middle, like say Python, you can think of them as living on a spectrum.
0:16 So let's put some of the common languages that you might know about and that we're going
0:20 to look at in just a moment on the spectrum to see where they lie on this dynamic versus strongly typed scale.
0:28 Let's start with the strongly typed ones. You might think C++. Now there's a very strongly typed language. Not sure if you have experience with it.
0:37 Many people do. You always have to say the type. If you want to have a number, you have to say int x, or it can be an unsigned int x,
0:46 or it can be a floating point or a more precise floating point in the form of a double. You have a class, you have to say the type.
0:52 You're always expressing what the type is. Those are a pointer to an integer or a pointer to a pointer of integer types.
1:02 Now, C++ is not all the way to the right here on this spectrum, as you can see, but it is
1:08 certainly towards the strongly typed side, which is the right hand side of this picture. On the far other side, we have languages like JavaScript.
1:18 JavaScript barely has a type system at all as a prototypical inheritance thing. Yes, so there are kind of ways to have classes, but for the most part,
1:28 you don't really have types over there. You have kind of dictionaries of things. And if you go and ask them, what are you?
1:36 They will say, I'm an object. They won't say, I'm a car derived from a vehicle class. No, they just say object.
1:46 Okay, so really super untyped in the JavaScript world. We could throw Ruby over there as well. Ruby's a little more typed, I believe.
1:54 I don't have a ton of experience, but it's certainly in the dynamic side of the scale. Back on the strongly typed side,
1:59 I would put C#, Microsoft's .NET language that's kind of a derivative of C and Java over here on this, and actually more strongly typed than C++.
2:11 For example, in C++, you can have a pointer, but then assign it to an integer because, hey, pointers are numbers, and so that's just that.
2:18 Or you can say if the pointer, And if the pointer is not falsy, it will become, you know, an evaluate to true.
2:26 Whereas in C#, things like if statements and while statements can only be true Boolean
2:30 expressions, you can't assign a pointer to an integer, it would say these are not the same things.
2:36 In fact, there's some pretty interesting stuff about some things being values and some things being references.
2:42 But let's just say a little bit more strongly typed than C++. Swift, Apple's programming language to replace Objective-C,
2:51 is even more strongly typed than C#. Because in Swift, you have the ability to say, this thing is a pointer, but this particular one pointer,
3:03 it can have a null value. This one is not nullable. You can't even assign null or nil in their syntax, their language over there.
3:12 So there's some really strong typing over there, especially around nullability and whether or not you're allowed to set something to
3:20 none from Python, null in most languages, nil in Swift. Interestingly, Swift takes a lot of hints from how Python works, but is also extremely
3:29 typed in this way. So where does Python fall on this spectrum? Well it actually falls way on the left by default.
3:37 Traditional, original, OG Python is right there with Ruby, just above JavaScript, right?
3:44 For example, if in Python I had something that was actually an integer and was actually
3:50 a list and I tried to add them, I'll get an exception that says type integer and type
3:55 list cannot be added together, unlike JavaScript, which will give you some weird, nonsensical answer and it'll just try to make them work anyway.
4:03 So in that sense, Python is more strongly typed. The things know what they are and when they interact with each other, they use their type
4:10 information to know whether or not that's allowed. But from a programming language perspective,
4:15 you look at both of them and they have no type information really going on. So Python's
4:19 basically over there hanging out with his friend JavaScript and Ruby. The original one.
4:25 There are trends, there are languages that try to take some of the things on the left
4:31 and make them a little bit stricter for building large scale applications for using tools that
4:36 understand type information that can help you say, auto complete your, your code statements
4:41 without actually having to go to the docs to figure out what the heck does this thing
4:44 return. One of them would be TypeScript. So TypeScript is a strongly typed programming
4:50 language that resembles more C#, less JavaScript, but is still also JavaScript.
4:57 And in some ways, like, not really a superset, but something that transpiles when you compile
5:03 it becomes JavaScript, a very different looking thing than what you write. But this has type information and is pretty strict.
5:11 For example, if the types mismatch, the thing won't compile, you don't get the JavaScript file, you can't run it.
5:17 So unlike Python, where it ignores the type information at runtime, this actually matters.
5:23 If you get the types wrong in TypeScript, the thing won't work, it won't compile, it doesn't just give you a warning or something along those lines.
5:31 We can also do the same thing with Python without transpiling it to something else,
5:36 but to add this type information to the language that is previously the dynamic type dynamically typed version.
5:44 So back in Python 3.5, a bunch of features were added under this general category of
5:49 type hints to allow us to put typing information right in the language.
5:54 And that brings Python way closer to TypeScript just to the left, because if you get the types
5:59 wrong, it still runs. It's not like TypeScript, where if you get the type wrong, or any other
6:04 strongly typed ones, it doesn't work. But it's in the same general category, we take
6:08 this dynamic language, we add type information to it, it becomes much more strongly typed,
6:14 the tools can know more about your code. And you can also run linters that understand those
6:20 types, you get a little bit better experience with this language. So we saw it's optional
6:25 and gradual, which is pretty awesome. So moving Python from this super dynamic version towards
6:31 but not all the way to the strongly typed languages. Well, that's what this course is about.


Talk Python's Mastodon Michael Kennedy's Mastodon