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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One area where Python is a little bit lacking is in constants. That is, I want to create a variable whose value never changes.
0:09 Maybe I want to create a variable for pi. You don't want people to reassign pi, do you?
0:15 I think even in the early days, you could reassign true and false to be like the opposite of each other if you wanted to cause trouble.
0:22 All sorts of weird things like that. So Python is not very strong in terms of its constant, its ability to express constant values that can't change.
0:32 So I'll have a const one here, or how about not const one. This is gonna be some value. And again, we're just gonna print out these things
0:44 so they don't have any issues. Now we can say not a const is other value. This is regular variable behavior. However, in Python, there's a convention
0:57 that if you want to have a constant value, you have a way of expressing that, sort of like the underscore field
1:05 represents a protected one you shouldn't use, but you can still see, right? There's some conventions around constants as well.
1:12 We'll have this constant two. This will be fixed value. And let's say that this is an implicit conventional constant.
1:26 So if we set it to no longer fixed, what do we see? Not very much, right? There's no warnings that this has happened.
1:38 Should have a warning, I guess, here, even though it doesn't, but it doesn't kind of follow the convention, right?
1:45 Well, in the Python type system, you can express something as a constant. Remember, the typing doesn't affect runtime, generally speaking.
1:55 So even doing so won't actually make it a constant, but you can detect it with some of the linting tools as well as PyChart.
2:04 We'll call this const three and we can express this as just saying final.
2:11 So typing that final here, and what values are going to have really a constant sort of so it's an explicit constant in the type system.
2:20 Let's print out the thing again, just so we don't get that false warning here.
2:24 go, this should not change. And here we can see that there is an error, the little action
2:31 thing goes away. It says const3 is final and could not be reassigned. Well, when you say
2:37 could not, what exactly? What about could not do you mean there? It looks like it did
2:43 change even though it's really a constant sort of should not. Let's change this word
2:48 to final as a constant and should not be reassigned. But you know, whatever, this is now an error
2:58 in the type system, right? Pretty much all the time when you see in the type system,
3:02 that means, well, it's still going to run along with its valid Python. But here's a
3:06 really nice way that we can go and express constants using typing not final, as usual,
3:11 I would normally write this, but just to be super explicit for everyone that it's coming out of the typing module, I'll write it like that.
3:19 So we can see that constants get a little bit of an upgrade. You can pair the naming convention of all caps with the typing of final, kind of bring
3:28 those two things together and get a little more support to say you really shouldn't be messing with this.
3:32 And you can see awesome editors such as PyCharm will say, and you shouldn't be messing with


Talk Python's Mastodon Michael Kennedy's Mastodon