Decorator to print Function call details in Python
Last Updated :
08 May, 2020
Decorators in Python are the design pattern that allows the users to add new functionalities to an existing object without the need to modify its structure. Decorators are generally called before defining a function the user wants to decorate.
Example:
Python3
# defining a decorator
def hello_decorator(func):
# inner1 is a Wrapper function in
# which the argument is called
# inner function can access the outer local
# functions like in this case "func"
def inner1():
print("Hello, this is before function execution")
# calling the actual function now
# inside the wrapper function.
func()
print("This is after function execution")
return inner1
# defining a function, to be called inside wrapper
def function_to_be_used():
print("This is inside the function !!")
# passing 'function_to_be_used' inside the
# decorator to control its behavior
function_to_be_used = hello_decorator(function_to_be_used)
# calling the function
function_to_be_used()
Output:
Hello, this is before function execution
This is inside the function !!
This is after function execution
Note: For more information, refer to
Decorators in Python
Decorator to print Function call details
Let's consider a scenario where you have written a very lengthy code and want to know the function call details. So what you can do is scroll through your code each and every time for different functions to know their details or you can work smartly. You can create a decorator that can print the details of any function you want.
To do this the functions in Python certain attributes. One such attribute is
__code__
that returns the called function bytecode. The
__code__
attributes also have certain attributes that will help us in performing our tasks. We will be using the
co_varnames
attribute that returns the tuple of names of arguments and local variables and
co_argcount
that returns the number of arguments (not including keyword-only arguments, * or ** args). Let's see the below implementation of such decorator using these discussed attributes.
Example:
Python3
# Decorator to print function call
# details
def function_details(func):
# Getting the argument names of the
# called function
argnames = func.__code__.co_varnames[:func.__code__.co_argcount]
# Getting the Function name of the
# called function
fname = func.__name__
def inner_func(*args, **kwargs):
print(fname, "(", end = "")
# printing the function arguments
print(', '.join( '% s = % r' % entry
for entry in zip(argnames, args[:len(argnames)])), end = ", ")
# Printing the variable length Arguments
print("args =", list(args[len(argnames):]), end = ", ")
# Printing the variable length keyword
# arguments
print("kwargs =", kwargs, end = "")
print(")")
return inner_func
# Driver Code
@function_details
def GFG(a, b = 1, *args, **kwargs):
pass
GFG(1, 2, 3, 4, 5, d = 6, g = 12.9)
GFG(1, 2, 3)
GFG(1, 2, d = 'Geeks')
Output:
GFG (a = 1, b = 2, args = [3, 4, 5], kwargs = {'d': 6, 'g': 12.9})
GFG (a = 1, b = 2, args = [3], kwargs = {})
GFG (a = 1, b = 2, args = [], kwargs = {'d': 'Geeks'})
Similar Reads
How to use Function Decorators in Python ? In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu
3 min read
How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde
3 min read
How to Call a C function in Python Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.First, let's write one simple function using C and generate a
2 min read
How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de
5 min read
Passing function as an argument in Python In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, decorators and lambda expressions. By passing a function as an argument, we can modify a functionâs behavior dynamically
5 min read