Assign Function to a Variable in Python
Last Updated :
03 Jul, 2024
In this article, we are going to see how to assign a function to a variable in Python. In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability.
Implementation
Simply assign a function to the desired variable but without () i.e. just with the name of the function. If the variable is assigned with function along with the brackets (), None will be returned.
Syntax:
def func():
{
..
}
var=func
var()
var()
Example:
Python
def a():
print("GFG")
# assigning function to a variable
var=a
# calling the variable
var()
Output:
GFG
The following programs will help you understand better:
Example 1:
Python
# defined function
x = 123
def sum():
x = 98
print(x)
print(globals()['x'])
# drivercode
print(x)
# assigning function
z = sum
# invoke function
z()
z()
Output:
123
98
123
98
123
Example 2: parameterized function
Python
# function defined
def even_num(a):
if a % 2 == 0:
print("even number")
else:
print("odd number")
# drivercode
# assigning function
z = even_num
# invoke function with argument
z(67)
z(10)
z(7)
Output:
odd number
even number
odd number
Example 3:
Python
# function defined
def multiply_num(a):
b = 40
r = a*b
return r
# drivercode
# assigning function
z = multiply_num
# invoke function
print(z(6))
print(z(10))
print(z(100))
Output:
240
400
4000
Assign Function to a Variable in Python – FAQs
How to set a function as a variable?
You can set a function as a variable by assigning the function to a variable name without calling it (i.e., without parentheses).
Example:
def greet():
print("Hello, World!")
# Assigning the function to a variable
greeting = greet
# Calling the function using the new variable
greeting() # Output: Hello, World!
How to store a function in a variable in Python?
To store a function in a variable, simply assign the function to the variable without using parentheses.
Example:
def add(a, b):
return a + b
# Storing the function in a variable
operation = add
# Using the variable to call the function
result = operation(5, 3)
print(result) # Output: 8
Can a function change a variable in Python?
Yes, a function can change a variable in Python if the variable is mutable (e.g., lists, dictionaries). For immutable types (e.g., integers, strings), the function can modify the variable locally, but it won’t change the original variable outside the function unless you return and reassign it.
Example with a mutable type:
def modify_list(my_list):
my_list.append(4)
nums = [1, 2, 3]
modify_list(nums)
print(nums) # Output: [1, 2, 3, 4]
Example with an immutable type:
def increment(x):
x += 1
return x
num = 10
num = increment(num)
print(num) # Output: 11
Can a function be assigned to another variable?
Yes, a function can be assigned to another variable, and the new variable can be used to call the function.
Example:
def multiply(a, b):
return a * b
# Assigning the function to another variable
product = multiply
# Using the new variable to call the function
result = product(5, 4)
print(result) # Output: 20
Please Login to comment...