Python | Accessing variable value from code scope
Last Updated :
08 May, 2023
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
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).
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