Open In App

Accessing Python Function Variable Outside the Function

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function without using “Global”.

Accessing Function Variable Outside the Function in Python

Below, are the ways to access a function variable outside the function without using Global in Python.

  • Return the Variable
  • Using Function Parameter
  • Using a Class
  • Using a Decorator

Access Function Variable Outside the Function by Returning the Variable

In this approach, the function returns the value of the variable, allowing access to the variable outside the function by assigning the result to a variable. The function get_variable() defines a variable var with the value 42 and returns it outside the function without using Global.

Python3




def get_variable():
    var = 42
    return var
  
# Accessing the function variable outside the function
result = get_variable()
print(result)
  


Output

42

Python Access Variable Outside the Function Using Function Parameter

In this approach the function takes a variable as a parameter, modifies it, and returns the modified value. The original variable is passed to the function. The function modify_variable(var) takes a variable as a parameter, increments its value by 10, and returns the modified value. By passing an original variable to the function and assigning the result to another variable (modified_var), the modified value (15) is accessible and printed outside the function.

Python3




def modify_variable(var):
    var += 10
    return var
  
# Define a variable outside the function
original_var = 5
  
# Passing the variable to the function
modified_var = modify_variable(original_var)
print(modified_var)


Output

15

Accessing Function Variables Outside Scope Using a Class

In this approach we create an instance of the class, the variable becomes an attribute of that instance, making it accessible without the need for the global keyword. In the example, a class VariableHolder is defined with an __init__ method initializing the variable to 42.

Python3




class VariableHolder:
    def __init__(self):
        self.var = 42
  
# Creating an instance of the class
holder = VariableHolder()
  
# Accessing the variable through the instance
print(holder.var)


Output

42

Access a Function Variable Outside the Function Using a Decorator

In this approach, a decorator function (store_variable) is defined, which takes another function as an argument. The decorator wraps the original function, storing its result as an attribute of the wrapper function. When the decorated function is called, it not only returns the result but also makes the result accessible through the attribute. In the example, the @store_variable decorator is applied to the get_variable function, and the variable’s value can be accessed using the attribute get_variable.variable.

Python3




def store_variable(func):
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        wrapper.variable = result
        return result
    return wrapper
  
@store_variable
def get_variable():
    return 42
  
# Accessing the variable through the decorated function
get_variable()
print(get_variable.variable)


Output

42



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads