Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Handling Constants

00:00 Now that you know how to create constants in Python, it’s time to learn how to handle and organize them in a real-world project. You can use several approaches or strategies to this end.

00:10 For example, you can put your constants in the same file as the code that uses them, a dedicated module for project-wide constants, a configuration file, or environment variables.

00:23 In these two sections of the course, you’ll write practical examples that demonstrate these strategies for maintaining constants appropriately. The first and probably most natural strategy to organize and manage your constants is to define them together with the code that uses them.

00:39 With this approach, you’ll be defining the constants at the top of the module that contains the related code. Let’s say you’re creating a custom module to perform calculations, and you need to use math constants such as Pi, Euler’s number, and a few others.

00:54 In this case, you could do something similar to what’s seen on screen. Here you define your constants in the same module where the code is using them. First, the imports are performed, then the constants are defined,

01:11 and after them the custom calculation functions are defined.

01:31 Putting constants together with code is a quick and appropriate strategy for narrow scope constants that are only relevant to a single module in a given project.

01:40 In this case, you probably won’t be using the constants outside the containing module itself. If you want to explicitly communicate that a constant should be used in its containing module only, you can add a leading underscore to its name, similar to what’s seen on screen.

01:56 This leading underscore labels the name as non-public, which means that the user’s code shouldn’t use this name directly. Another common strategy for organizing and managing your constants is creating a dedicated module in which to put them all. This strategy is appropriate for constants that are used in many modules and even packages across a given project.

02:19 The central idea is to create an intuitive and unique namespace for constants. To apply this strategy to your calculations example, you can create a Python package using the structure seen on screen. The __init__.py file will turn the calc/ directory into a Python package.

02:38 Then you can add the content seen on screen to the constants.py file.

02:47 Once you’ve saved this code, you can import the module wherever you need to use any of your constants.

03:00 Note that you import the constants module directly from the calc/ package using a relative import. Then you use the fully qualified names to access any required constants in the calculations.

03:14 This practice improves your communication of intent. It’s now completely clear that Pi and Euler number are constants in your project. Because of the constant prefix

03:25 to use the calculations module, you can do it in the way seen on screen.

03:35 Now, your calculations module lives inside the calc package. This means that if you want to use the functions in calculations, then you need to import calculations from calc.

03:44 But note that it’s also possible to import the functions directly.

04:01 In the next section of the course, you’ll take a look at some more real-world techniques for dealing with constants in your code, configuration files and environment variables.

Become a Member to join the conversation.