Assign Function to a Variable in Python (original) (raw)

Last Updated : 11 Jun, 2026

Functions are first-class objects, which means they can be assigned to variables just like any other value. Once a function is assigned to a variable, the function can be called using either the original function name or the variable.

Python `

def show(): print("GFG")

func = show func()

`

**Explanation:

**Note: When assigning a function to a variable, use the function name without parentheses. Writing func = show assigns the function itself, while func = show() executes the function and assigns its return value.

Syntax

def function_name(parameters):
# function body
pass

variable_name = function_name
variable_name(arguments)

Where:

Examples

**Example 1: The following example demonstrates assigning a function to a variable and calling it using that variable. It also shows the difference between local and global variables.

Python `

x = 123

def display(): x = 98 print(x) print(globals()['x'])

print(x)

a = display a() a()

`

**Explanation:

**Example 2: The following example assigns a function that accepts a parameter to a variable and calls it using that variable.

Python `

def check(num): if num % 2 == 0: print("Even number") else: print("Odd number")

a = check a(67) a(10) a(7)

`

Output

Odd number Even number Odd number

**Explanation:

**Example 3: The following example assigns a function that returns a value to a variable and calls it using that variable.

Python `

def multiply(num): return num * 40

a = multiply print(a(6)) print(a(10)) print(a(100))

`

**Explanation: