Have you ever stored a number, a string, or a list in a Python variable? Of course, you have. It’s one of the first things you learn. But what if I told you that you can also store an entire function inside a variable? This might sound strange at first, but it’s a fundamental and powerful concept in Python.
This idea is the gateway to understanding advanced Python features like decorators and callbacks. In this guide, we’ll demystify this concept by exploring the simple yet profound idea that in Python, functions are objects. By the end, you’ll be confidently assigning functions to variables and using them in new, flexible ways.
What Does “Functions are Objects” Mean?
In Python, everything is an object. The number 5 is an object of class int. The string "hello" is an object of class str. Similarly, when you define a function using def, Python creates an object of class function.
Because functions are objects, they can do many of the same things other objects can do. They can be:
- Passed as an argument to another function.
- Returned from a function.
- And, most importantly for us right now, assigned to a variable.
This last point is our focus today. Let’s see it in action.
Proof 1: Assigning a Function to a Variable
Let’s start by creating a simple function.
# Defining a simple function
def greet():
print("Hello, World!")If you run this code, nothing will happen. We’ve defined the function but haven’t called it yet. The function greet is now an object sitting in memory, ready to be used.
Now, for the magic. We can assign this function to a new variable, just like we would with any other piece of data.
# Assigning the function object to a variable 'x'
x = greetPay close attention to the syntax. We write x = greet, not x = greet(). Why?
greetrefers to the function object itself.greet()is a call to the function, which would execute it and assign its return value (which isNonein this case) tox.
We want x to hold the function, not the result of calling it.
Proof 2: Checking the Type of the Variable
How can we be sure that x is now actually storing a function? We can ask Python directly using the type() function.
Let’s check the type of x after our assignment.
# Checking the type of the variable 'x'
print(type(x))When you run this code, you’ll see the following output:
<class 'function'>
This is the proof! The variable x is not an integer or a string; its type is function. This confirms that the greet function object has been successfully stored inside x.
Proof 3: Calling the Function Through the Variable
Here’s where the concept becomes truly powerful. If x now holds the greet function, can we use x to call that function? Absolutely.
You can call the function using the variable x with the same parentheses syntax.
# Calling the original function
greet()
# Calling the function using the variable 'x'
x()When you run this code, you’ll see the output printed twice:
Hello, World! Hello, World!
This demonstrates that both greet and x are now references to the exact same function object. They are interchangeable names for the same block of code.
Why Is This Useful? A Simple Analogy
Think of a function as a recipe for a cake. The def greet() step is you writing that recipe down on a card and naming it “Greet’s Cake Recipe.”
Assigning it to a variable (x = greet) is like making a photocopy of that recipe card and labeling the copy “X’s Cake Recipe.”
You can now use either card to bake the exact same cake. It doesn’t matter which one you use; the result is identical. This ability to have multiple names for the same function is the foundation for many advanced programming techniques that make code more modular and reusable.
Ready to Go from Basics to Pro?
You’ve just taken a big step into understanding what makes Python such a flexible and powerful language. Grasping that functions are first-class objects opens up a whole new world of programming paradigms, from simple function aliasing to sophisticated decorators.
If you’re excited by this concept and want to build a deep, practical understanding of Python through structured learning, expert guidance, and real-world projects, our comprehensive course is designed for you.
If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.
Conclusion
In this post, you learned a core tenet of Python: functions are objects. We proved this by:
- Assigning a function to a variable
x. - Showing that the type of
xisfunction. - Successfully calling the function using the variable
x.
This concept is not just an academic curiosity; it’s a fundamental tool used by Python developers everywhere. Keep practicing by trying this with your own functions, and you’ll solidify this crucial knowledge.
Common Questions About Functions as Objects
Can I assign a function with arguments to a variable?
Yes, absolutely. The process is identical. For example, if you have def greet(name): print(f"Hello, {name}"), you can assign it with my_func = greet. To call it, you would then use my_func("Alice"), which would pass the argument correctly.
What’s the difference between x = greet and x = greet()?x = greet assigns the function object itself to x. x = greet() calls the function, executes its code, and then assigns whatever the function returns to x. In our example, greet() returns None, so x would become None.
Is this the same as a function alias?
Essentially, yes. When you assign a function to a variable, you are creating an alias for that function. Both the original name and the new variable name point to the same function object in memory.














