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

Unlock This Lesson

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

Unlock This Lesson

Avoiding Global Variables

00:00 Avoiding Global Variables in Your Code and Functions. As you learned previously. Relying on global variables in your functions and code can cause a few undesirable effects.

00:12 These variables can make your code harder to understand, test, and debug. They can also lead to less maintainable and reusable code. So you must use global variables sparingly, with care and control.

00:25 Fortunately, there are some neat strategies that you can use to minimize the need for global variables in your code. Perhaps the most intuitive strategy for avoiding global variables is to use global constants.

00:38 Unlike variables, constants must not change their value during code execution, so they promote the safe use of global names.

00:48 Python doesn’t support strict constants or non-reassignable names. To define a constant in Python, you should use uppercase letters with underscores, but this is just a naming convention and doesn’t actually prevent developers from assigning new values to your constant.

01:04 So you need to be careful and never write code that changes the values of those constants. To illustrate how to use constants, instead of using global variables, let’s say you have some general configuration parameters and want to make them accessible from all parts of the program.

01:20 In this case, you can use global constants. You’ll typically define constants at the top of your module, right after the imports. On screen are some example constants in Python. You can use constants to store values that won’t change during your program’s execution.

01:39 Constants can help you to prevent accidental modification of values. They also improve code readability and maintainability. Once the constants have been defined, you can access them in your functions across your code.

01:53 You just need to make sure to keep them unchanged.

01:57 On screen, you’ll see a function that loads an image from NASA’s main API page.

02:04 In this example, you import webbrowser and requests. webbrowser is from the Python standard library and allows you to quickly open URLs in your default browser.

02:14 requests allows you to make HTTP requests to the target REST API.

02:21 If you’d like to learn more about accessing REST APIs in your code, check out this Real Python tutorial.

02:41 Inside load_earth_image(), you access the three global constants directly, just as you would do with any global variable in Python, but you don’t change the value of any of the constants inside the function.

02:54 Now, you can reuse these constants in other related functions as well. Using constants in this way allows you to improve your code’s readability and maintainability.

03:05 Additionally, because they’re constant, you don’t have to keep track of their current values when you are debugging or testing the code, you’ll always know their values.

03:19 To give this code a try you can run it as seen on screen.

03:27 The call to load_earth_image() will get you the image that NASA has for July the 29th, 2023. The image will open in your default browser. You can experiment with other dates and get your own images.

03:43 Writing functions that take arguments and return values instead of relying on global variables is another good strategy to avoid the use of global variables in your code.

03:53 These types of functions are known as pure functions and are the foundation of functional programming, which is a popular programming paradigm. Pure functions are often totally decoupled from other parts of your code.

04:06 More importantly, they don’t produce side effects such as changing the state of global variables. These features make them more reusable and easier to understand, debug, test, and maintain.

04:20 As an example, here’s a new implementation of our old friend increment(), which doesn’t use a global variable inside the function. In this case, the function just takes a value as an argument and returns it incremented by one.

04:40 This new version of increment doesn’t rely on external values from global variables, so its result will only depend on its input argument. Every time you run this function with the same argument, you’ll get the same result, which makes this function easier to test, understand, and debug.

04:58 The function is completely decoupled from any other part of the program, so you can reuse it even in a different program. In this example, counter is mutated only in the global scope.

05:10 You don’t perform interscope mutations, so this makes the code safer and easy to understand.

05:18 In the next section of the course, you’ll look at how you can use classes, methods, and attributes to avoid the use of global variables in your code.

Become a Member to join the conversation.