Challenge: Given is function f
. How to assign the function to variable g
, so that you can call g()
and it runs function f()
?
def f(): print('Finxter') # Assign function f to g g()
Your desired output is function f
‘s output:
Finxter
How to accomplish this in the most Pythonic way?
Overview: We examine two methods to accomplish this challenge. You can run them in our interactive Jupyter notebook, or read the following explanations first.
Let’s dive into the two methods!
Method 1: Assign Function Object to New Variable Name
A simple way to accomplish the task is to create a new variable name g and assign the function object f to the new variable with the statement f = g.
Here’s the code snippet that solves the problem:
# Method 1 def f(): print('Finxter') # Assign function f to g g = f # Run g g()
The output is the expected output of function f
:
Finxter
The reason is that both variables point to the same function object in memory. You can see this in the following memory visualizer tool:
Click next to explore how the memory changes with the unfolding code.
Method 2: Lambda Wrapper Function
You can also use a lambda
function that is an anonymous function object without name. The lambda function takes zero or more arguments and returns the result of the expression after the colon. In our case, the lambda function simply wraps the original function f()
which means it internally runs f()
and returns f
‘s return value. The new function object behaves similarly to f
.
Here’s how this method solves our challenge:
# Method 2: Lambda Wrapper Function def f(): print('Finxter') # Create Lambda Wrapper Function g = lambda : f() # Run g g()
The output is the same:
Finxter
Let’s test how well you understood the introduced concepts in this article.
[Python Puzzle] Test Your Renaming Functions Skills
Python puzzles help you train your rapid code understanding skills. Can you figure out this code puzzle?
def func(x): return x+1 f = func print(f(2) + func(2))
What is the output of this code snippet?
Explanation: Redundant code indicates poor programming style. An excellent way to avoid redundant code is given by functions.
Functions generalize a certain behavior and encapsulate a sequence of program instructions. The ideal function solves a single semantic high-level goal. For instance, you can encapsulate a complex task, such as searching the web, into a function. In this way, the complex task becomes a simple one-liner: calling the function. Functions enable others to reuse your code and allows you to reuse other people’s code. You are standing on the shoulders of giants.
You can define a function with the keyword def
, followed by a name and the arguments of the function. The Python interpreter maintains a symbol table that stores all function definitions. In this way, the interpreter can relate each occurrence of the function name to the defined functionality.
It is possible to rename your function in the symbol table. This is as simple as assigning the function name to a new variable (e.g. f
). Upon the function call, the Python interpreter will find the renamed function in the table and execute it. This makes your life easier when working with other people’s code which you are not allowed to change.
You can check the output of this puzzle on our interactive puzzle app Finxter.com. Are you a master coder?