Conditional Decorators in Python Last Updated : 10 Jul, 2020 Comments Improve Suggest changes Like Article Like Report 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. Python3 1== 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()) 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. Python3 1== 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()) Output: GEEKSFORGEEKS Comment More infoAdvertise with us Next Article Conditional Decorators in Python V vanshikagoyal43 Follow Improve Article Tags : Python Python Decorators Practice Tags : python Similar Reads Conditional Inheritance in Python It happens most of the time that given a condition we need to decide whether a particular class should inherit a class or not, for example given a person, if he/she is eligible for an admission in a university only then they should be a student otherwise they should not be a student. Let's consider 3 min read Python Logical Operators Python logical operators are used to combine conditional statements, allowing you to perform operations based on multiple conditions. These Python operators, alongside arithmetic operators, are special symbols used to carry out computations on values and variables. In this article, we will discuss l 6 min read Conditional Statements in Python Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations.If Conditional Statement in PythonIf statement is the simplest form of a conditional sta 6 min read Python 3 - Logical Operators Logical Operators are used to perform certain logical operations on values and variables. These are the special reserved keywords that carry out some logical computations. The value the operator operates on is known as Operand. In Python, they are used on conditional statements (either True or False 4 min read Comparison Operators in Python Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters 4 min read Like