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

  • Home
  • SEARCH
  • 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 3335696
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:02:53+00:00 2026-05-18T00:02:53+00:00

Sorry for long question, but I don’t know how to make it shorter. 1.

  • 0

Sorry for long question, but I don’t know how to make it shorter.

1. Decorators without args

Implementation 1.1 (through function)

import time
import functools

def pause(f):
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        time.sleep(1)
        return f(*args, **kwargs)

    return wrapper

@pause
def func(x, y):
    """desc"""
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on function func in module __main__:

func(*args, **kwargs)
    desc

It’s "classical" implementation – a lot of articles contain it.

-breaks function signature (can be solved by nonstandard module decorator)

-you must use functools for saving name and doc string of function (not very big problem, but slightly more code and magic)

+you can modify function args (not always necessary)

+-? Anything else?

Implementation 1.2 (through function)

import time

def pause(f):
    time.sleep(1)
    return f

@pause
def func(x, y):
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on function func in module __main__:

func(x, y)
    desc

? Why it implementation rarely used in articles and examples? What have I missed?

-you can’t modify function args

+not breaks function signature

+less code

+-? Anything else?

Implementation 1.3 (through classes)

import time

class pause(object):
    def __init__(self, f):
        self.f = f
    
    def __call__(self, *args, **kwargs):
        time.sleep(1)
        return self.f(*args, **kwargs)

@pause
def func(x, y):
    """desc"""
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on pause in module __main__ object:

class pause(__builtin__.object)
 |  Methods defined here:
 |  
 |  __call__(self, *args, **kwargs)
 |  
 |  __init__(self, f)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

IMHO, ugly.

Implementation 1.4 (through classes)

import time

class pause(object):
    def __call__(self, f):
        time.sleep(1)
        return f

@pause()
def func(x, y):
    """desc"""
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on function func in module __main__:

func(x, y)
    desc

-you can’t modify function args

-nonstandard syntax for decorator use

+not breaks function signature

+-less code (but more than implementation 2 🙂 )

+-? Anything else?

2. Decorators with args

Implementation 2.1 (through function)

import time
import functools

def pause(t):
    def wrapper(f):
        @functools.wraps(f)
        def tmp(*args, **kwargs):
            time.sleep(t)
            return f(*args, **kwargs)
        return tmp

    return wrapper

@pause(1)
def func(x, y):
    """desc"""
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on function func in module __main__:

func(*args, **kwargs)
    desc

It’s "classical" implementation – a lot of articles contain it.

-breaks function signature

-nested is worse than flat

-you must use functools for saving name and doc string of function

+you can modify function args

+-? Anything else?

Implementation 2.2 (through function)

import time

def pause(t):
    def wrapper(f):
        time.sleep(t)
        return f
    return wrapper

@pause(1)
def func(x, y):
    """desc"""
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on function func in module __main__:

func(x, y)
    desc

? Why it implementation rarely used in articles and examples? What have I missed?

-you can’t modify function args

+not breaks function signature

+less code

+-? Anything else?

Implementation 2.3 (through classes)

import time

class pause(object):
    def __init__(self, darg):
        self.darg = darg
    
    def __call__(self, f):
        time.sleep(self.darg)
        return f

@pause(1)
def func(x, y):
    """desc"""
    return x + y

print func(1, 2)
help(func)

Output:

3
Help on function func in module __main__:

func(x, y)
    desc

-you can’t modify function args

+not breaks function signature

+-less code (but more than implementation 2 🙂 )

IMHO+ little clearer than nested functions

+-? Anything else?

Questions in one place

  1. Why implementations 1.1/2.1 are more common in articles and examples as compared with implementations 1.2/2.2?
  2. What are the pros and cons I missed?
  3. Did I missed something else?
  • 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-05-18T00:02:53+00:00Added an answer on May 18, 2026 at 12:02 am

    It sure is good that the implementations 1.2 & 2.2 are “less common than” 1.1 and 2.1, because they do different things. In fact, I wouldn’t call them “decorators”, because they don’t really wrap (“decorate”) the function. Instead, they only perform their action once, at the moment the decorated function is parsed. That is, their action (sleep in your case) is performed at function definition time, not at function invocation time.

    Try invoking your decorated functions several times and you will see the difference.

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

Sidebar

Related Questions

Sorry if this sounds like a really stupid question, but I need to make
First of all, sorry for the too long question. I know that there are
Sorry for the long question title. I guess I'm on to a loser on
Sorry in advance for the long question. What I'm really interested in is a
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing
Note: I'm sorry if this is an extremely simple question but I'm somewhat obsessive
Sorry if this is not a programming question, but I'm not really sure where
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry for my ignorance here, but when I hear the word webserver, I immediately

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.