WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Python Β· Basic Β· question 15 of 100

What are decorators in Python? Provide a simple example.?

πŸ“• Buy this interview preparation book: 100 Python questions & answers β€” PDF + EPUB for $5

In Python, a decorator is a special type of function that can modify the behavior of another function without changing its source code. Decorators are used to add functionality to functions, methods, or classes, by wrapping them with other functions that can add functionality before or after the original function is called.

Here is the basic syntax for a decorator:

def my_decorator(func):
        def wrapper():
            # do something before the original function is called
            result = func()
            # do something after the original function is called
            return result
        return wrapper
    
    @my_decorator
    def my_function():
        # do something
        return result

In this syntax, my_decorator is a decorator function that takes a function as an argument, and returns a new function called wrapper. The wrapper function adds some functionality before and after the original function my_function is called.

To use the decorator, we add the @my_decorator syntax before the original function my_function. This tells Python to pass my_function as an argument to the my_decorator function, and replace my_function with the new wrapper function that the decorator returns.

Here is an example of a simple decorator that adds some functionality before and after a function is called:

def my_decorator(func):
        def wrapper():
            print('Before the function is called.')
            result = func()
            print('After the function is called.')
            return result
        return wrapper
    
    @my_decorator
    def my_function():
        print('The function is called.')
        return 'Hello, world!'
    
    my_function()

In this example, we define a decorator called my_decorator that prints a message before and after the original function is called. We then define a function called my_function that prints a message and returns a string.

We apply the my_decorator decorator to my_function using the @my_decorator syntax, and then call my_function. When we call my_function, the decorator adds the extra functionality of printing messages before and after the function is called.

Decorators are a powerful tool in Python that can be used to modify the behavior of functions, methods, and classes in a flexible and dynamic way. By using decorators, you can write more reusable and modular code that is easy to read, understand, and maintain.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview β€” then scores it.
πŸ“ž Practice Python β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Python questions & answers β€” PDF + EPUB for $5

All 100 Python questions Β· All topics