Using Constants
00:00 Since this course is all about structuring your Python script in a way that makes it easier to change and conforms to best practices, one of the things that’s a good idea to look out for is not to use hard-coded values in your script, but use those as an opportunity to use constants instead. When I say constants, I mean constants that are variables that are defined in a single place so that the values they represent do not have to be copied and pasted in multiple places.
00:29 They are values that are designed not to be changed during execution. By Python convention, they are usually defined in all capitals, so they might look something like this.
00:39
You might define a path to a file as a Python string, but to signal to the readers of this code that this is a constant that should not be changed, the variable name FILE_PATH here would be all capitals. A good rule of thumb when thinking about constants is whenever you find yourself hard-coding a value or copying a value and pasting it in multiple places in your script, ask yourself whether this value could be a constant defined in a single place.
01:08
Let’s look at an example. In this file, the quote_generator.py file, we use rich to print a message, but you can also apply a style to that text. And the style is defined as a string, in this case it’s bold and it has the color of cyan, which is a sort of light blue.
01:30
Down here later on in the script, there’s another example of defining that style. Let’s see that in action before we make any changes. If you go over to the terminal, type python quote_generator.py.
01:45 The output is an inspirational quote, as well as a welcome message. And both of those are shown in the same color in this bold cyan color. If you wanted to change what color that message is, there’s actually two places in the script now that would need to change.
02:01 So if I wanted to change the style here from cyan to green on line 16, I also have to do it here on line 23. You would have to remember all the different places to change that color to make the entire output the same color. So if you make those changes and save the file,
02:19
go back to the terminal and rerun python space and then the name of the file, quote_generator.py, we’ve changed the color to green.
02:29
Because there are two places where we’re defining the same style, this is a great place to take those and refactor them as a single constant. So slightly further up in the script, create a new variable called TEXT_STYLE, but in all capitals to signal to the readers of this code that this is a constant that should not change.
02:51
And we’re going to make it equal to whatever style we like, let’s say "bold green". So it’s a Python string, "bold green". And then everywhere you’ve manually used this TEXT_STYLE, we’re going to replace it
03:08
with the new variable TEXT_STYLE. So that’s in the first print message here on line 18 to line 18 now.
03:18 And further down in the second message where we print the quotation,
03:24
in both of those cases, we’re going to replace the hard-coded value of "bold green" to just using this single variable. And that means if you want to change the color of the output, there’s only one place now you have to remember to change, which is up here.
03:39 If you remove green, let’s try a different color. Let’s try magenta, nice bright pink, save the file. That’s the only place you need to remember to change it now.
03:49
And if we go back to the terminal, rerun python space and the name of the file, the output is now a different color. So refactoring it with a constant has made it easier to change and more flexible in the future.
04:05 In the next lesson, we’re going to talk about another best practice, which is ensuring a consistent entry point for your script.
Become a Member to join the conversation.
