Search⌘ K
AI Features

Storing Function Reference in a Variable

Explore how Python treats functions as first-class objects by showing how to store function references in variables. Understand the similarities between variables and functions, how function objects behave, and how this concept supports flexible programming practices.

Variable vs. function initialization and usage

When you look at the way a variable is initialized and used in Python and compare it to the way a function is declared and used, you might easily assume that variables and functions are completely different things.

Python 3.8
a = 10
def square(x):
return x*x
b = square(a)
print(a)
print(b) # prints 100

Looking at the code, ...