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

Creating Global Variables

00:00 Creating Global Variables Inside a Function As you’ve already learned, you can use the global keyword or the built-in globals() function to access and modify a global variable inside a function.

00:13 What’s really interesting and probably surprising is that you can also use these tools to create global variables inside a function. Note that while you can create global variables inside functions using the global keyword or the globals() function, this is uncommon and it’s not a best practice.

00:31 It can make your code harder to understand and maintain. It’ll be better to define global variables at the module level outside of any functions. This way their scope will be clear and you won’t run the risk of getting a name error if you use the variable before calling the defining function. On screen, you can see an example where you create a global variable inside a function using the global keyword.

00:59 Here, the set_global_variable function uses the global keyword to define number as a global variable. When you call the function, number is defined and its value is set to seven.

01:12 After the call, you can access number from anywhere else in the program.

01:18 You can also use the globals() function to define new global variables inside your functions. This tool gives you additional flexibility, allowing you to create global variables dynamically.

01:34 Here, the dynamic_global_variable function uses the globals() function to define a new global variable using a name as a string and a value. The call to the function creates a new global variable called number with a value of 42.

01:53 For a more realistic example, let’s say you have a configuration file in JSON format. You want to process the file and load all of the configuration parameters directly into your global namespace, so you can use these parameters in other parts of your code as global variables.

02:11 The config.json file may look similar to the one seen on screen.

02:17 The file defines a database key whose value is a dictionary of keys and values. You’ll use this dictionary as the initial setting for the application database.

02:27 The file also defines an api key, a base URL, and a timeout for connecting to an API.

02:39 This code seen on screen takes care of loading the JSON file and creating the required set of global variables for you. After importing the built-in json module, you define set_config_key, which takes a key and value as arguments.

02:55 Then you use globals() to create a global variable using the key as the variable name and value as its value.

03:03 In the with statement, you open the configuration file for reading and assign the file object to the json_config variable. Then you use a for loop to process the loaded configuration and create new global variables for each key-value pair.

03:21 After you’ve completed this process, you can access all your configuration parameters as global variables in your code.

03:37 Even though defining global variables within your functions using either the global keyword or the globals() function is perfectly possible in Python, it’s not a best practice.

03:47 You must be careful when defining global variables in this way. Those variables become available to your entire program, so other pieces of your code can modify them.

03:57 Because of this, you can end up with code that’s difficult to understand, test, maintain, and debug. Maybe you are noticing a bit of a pattern. There are plenty of ways to use and define global variables in your functions, but does that mean that you should?

04:13 In the next section, you’ll explore this question further.

Become a Member to join the conversation.