Conditional Decorators in Python

In Python, decorators are functions or classes that wrap around a function as a wrapper by taking a function as input and returning out a callable. They allow the creation of reusable building code blocks that can either change or extend behavior of other functions.

Conditional Decorators

Given a condition, the idea here is to execute code or basically wrap a function using a decorator if a certain condition is met or true. There are two ways by which we can use a decorator conditionally.

Method 1: When the decorator decides how to wrap a function

  • In this case the function is passed to a decorator normally
  • Then based on the condition the decorator decides what to do with the code

The following program passes a function normally to the decorator then if the given condition is true the program returns the string in uppercase and if it is false it returns it in lower case.

filter_none

edit
close

play_arrow

link
brightness_4
code

condition = True
  
def conditional_decorator(func):
      
    def wrapper():
        oldstring = func()
          
        if condition:
            newstring = oldstring.upper()
        else:
            newstring = oldstring.lower()
          
        return newstring
      
    return wrapper
  
@conditional_decorator
def func():
    return 'geeKSfoRGEEks'
  
print(func())

chevron_right


Output:



'GEEKSFORGEEKS'

Method 2: In this, decorators are called only if a certain condition is met.

In the following program, the program takes user input to decide on the condition. If the user enters 1, the decorator is called and the string is returned in uppercase. If the user enters 2, again a decorator is called and the given string is returned in lowercase. Apart from this if any other number is entered the function is returned as it is without any modification.

filter_none

edit
close

play_arrow

link
brightness_4
code

def decorator1(func):
      
    def wrapper():
        oldstring = func()
        newstring = oldstring.upper()
        return newstring
      
    return wrapper
  
def decorator2(func):
      
    def wrapper():
        oldstring = func()
        newstring = oldstring.lower()
        return newstring
      
    return wrapper
  
cond = 1
  
if cond == 1:
    @decorator1
    def func():
        return 'GeeksFORGeeKs'
elif cond == 2:
    @decorator2
    def func():
        return 'GeeksFORGeeKs'
else:
    def func():
        return 'GeeksFORGeeKs'
      
print(func())

chevron_right


Output:

GEEKSFORGEEKS



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.