Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:41:48+00:00 2026-06-04T12:41:48+00:00

Is it possible to decorate a function conditionally? For example, I want to decorate

  • 0

Is it possible to decorate a function conditionally? For example, I want to decorate the function foo() with a timer function (timeit), but only when doing_performance_analysis condition is True, like this:

    if doing_performance_analysis:
      @timeit
      def foo():
        """
        Do something, e.g. sleep, and let timeit 
        return the time it takes
        """
        time.sleep(2)
    else:
      def foo():
        time.sleep(2)  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:41:49+00:00Added an answer on June 4, 2026 at 12:41 pm

    Decorators are simply callables that return a replacement, optionally the same function, a wrapper, or something completely different. As such, you could create a conditional decorator:

    def conditional_decorator(dec, condition):
        def decorator(func):
            if not condition:
                # Return the function unchanged, not decorated.
                return func
            return dec(func)
        return decorator
    

    Now you can use it like this:

    @conditional_decorator(timeit, doing_performance_analysis)
    def foo():
        time.sleep(2)  
    

    The decorator could also be a class:

    class conditional_decorator(object):
        def __init__(self, dec, condition):
            self.decorator = dec
            self.condition = condition
    
        def __call__(self, func):
            if not self.condition:
                # Return the function unchanged, not decorated.
                return func
            return self.decorator(func)
    

    Here the __call__ method plays the same role as the returned decorator() nested function in the first example, and the closed-over dec and condition parameters here are stored as arguments on the instance until the decorator is applied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to decorate a function, using a pattern like this: def deco(func): def
is it possible to easily and dynamically decorate an object? for example, lets say
Is it possible to decorate the class with; [Authorize(Roles = Admin)] But have it
is it possible to decorate a field of a LINQ generated class with [Column(IsDbGenerated=true)]
I want to write a python decorator to decorate a test function of a
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Goal: Make it possible to decorate class methods. When a class method gets decorated,
Suppose I want to decorate links to certain file types using an image. I
I would like to create an attribute I can decorate specific fields or properties

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.