Python | Accessing variable value from code scope Last Updated : 08 May, 2023 Comments Improve Suggest changes Like Article Like Report Sometimes, we just need to access a variable other than the usual way of accessing by it's name. There are many method by which a variable can be accessed from the code scope. These are by default dictionaries that are created and which keep the variable values as dictionary key-value pair. Let's talk about some of these functions. Method #1 : Using locals() This is a function that stores the values of all variables in local scope of function if in a function or of global scope if outside. Python3 # Python3 code to demonstrate working of # Accessing variable value from code scope # using locals # initialize variable test_var = "gfg is best" # printing original variable print("The original variable : " + str(test_var)) # Accessing variable value from code scope # using locals res = locals()['test_var'] # printing result print("Variable accessed using dictionary : " + str(res)) Output : The original variable : gfg is best Variable accessed using dictionary : gfg is best Method #2: Using globals() This is yet another function that maintains a dictionary of variables of global scope. Python3 # Python3 code to demonstrate working of # Accessing variable value from code scope # using globals # initialize variable test_var = "gfg is best" # printing original variable print("The original variable : " + str(test_var)) # Accessing variable value from code scope # using globals res = globals()['test_var'] # printing result print("Variable accessed using dictionary : " + str(res)) Output : The original variable : gfg is best Variable accessed using dictionary : gfg is best Method #3: Using a class Define a class MyClass with a class-level variable my_var set to the desired value.Define a function func2 that prints the value of MyClass.my_var.Call func2 to print the value of MyClass.my_var. Python3 class MyClass: my_var = 'gfg is best' def func2(): print(MyClass.my_var) func2() # Output: gfg is best Outputgfg is best Time complexity: O(1)Auxiliary Space: O(1) Method 4: using the built-in function vars(). Steps: Initialize a variable test_var with a string value.Print the original variable using the print() function.Use the vars() function to access the variable value from code scope and assign it to a new variable res.Print the result. Python3 # initialize variable test_var = "gfg is best" # printing original variable print("The original variable : " + str(test_var)) # Accessing variable value from code scope # using vars res = vars()['test_var'] # printing result print("Variable accessed using dictionary : " + str(res)) OutputThe original variable : gfg is best Variable accessed using dictionary : gfg is best Time complexity of this program is O(1) Auxiliary space required is O(1). Comment More infoAdvertise with us Next Article Python | Accessing variable value from code scope manjeet_04 Follow Improve Article Tags : Python Python Programs python-basics Practice Tags : python Similar Reads Accessing Python Function Variable Outside the Function In Python, function variables have local scope and cannot be accessed directly from outside. However, their values can still be retrieved indirectly. For example, if a function defines var = 42, it remains inaccessible externally unless retrieved indirectly.Returning the VariableThe most efficient w 4 min read Python Program to Find and Print Address of Variable In this article, we are going to see how to find and print the address of the Python variable. It can be done in these ways:Using id() functionUsing addressof() functionUsing hex() functionMethod 1: Find and Print Address of Variable using id()We can get an address using id() function, id() function 2 min read Declare variable without value - Python A variable is a container that stores a special data type. Unlike C/C++ or Java, a Python variable is assigned a value as soon as it is created, which means there is no need to declare the variable first and then later assign a value to it. This article will explore How to Declare a variable without 2 min read Variables under the hood in Python In simple terms, variables are names attached to particular objects in Python. To create a variable, you just need to assign a value and then start using it. The assignment is done with a single equals sign (=): Python3 # Variable named age age = 20 print(age) # Variable named id_number id_no = 4067 3 min read Python | Using variable outside and inside the class and method In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let's see, how to use and access these variables throughout the program. Variable defined outside the class: The variables that are defined outside the class can be accessed by any class or any me 3 min read Access Dictionary Values Given by User in Python Dictionaries are a fundamental data structure in Python, providing a flexible way to store and retrieve data using key-value pairs. Accessing dictionary values is a common operation in programming, and Python offers various methods to accomplish this task efficiently. In this article, we will explor 4 min read UnboundLocalError Local variable Referenced Before Assignment in Python Handling errors is an integral part of writing robust and reliable Python code. One common stumbling block that developers often encounter is the "UnboundLocalError" raised within a try-except block. This error can be perplexing for those unfamiliar with its nuances but fear not â in this article, w 4 min read How to Access Dictionary Values in Python Using For Loop A dictionary is a built-in data type in Python designed to store key-value pairs of data. The most common method to access values in Python is through the use of a for loop. This article explores various approaches to accessing values in a dictionary using a for loop. Access Dictionary Values in Pyt 2 min read Python program to create dynamically named variables from user input Given a string input, our task is to write a Python program to create a variable from that input (as a variable name) and assign it to some value. Below are the methods to create dynamically named variables from user input. Using globals() method to create dynamically named variables Here we are usi 2 min read Python Access Set Items Python sets are unordered collections of unique elements. Unlike lists or dictionaries, sets do not have indices so accessing elements in the traditional way using an index doesn't apply. However, there are still ways to work with sets and access their items effectively. This article will guide you 2 min read Like