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 5945873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:44:34+00:00 2026-05-22T16:44:34+00:00

Consider the following code fragment. def print_timing(func): import time def wrapper(*args, **kwargs): t1 =

  • 0

Consider the following code fragment.

def print_timing(func):
    import time
    def wrapper(*args, **kwargs):
        t1 = time.time()
        res = func(*args, **kwargs)
        t2 = time.time()
        print '%s took %0.3f s ~ %0.0f min and %0.1f sec' % (func.func_name, t2-t1, int(t2 - t1)/60, (t2-t1) % 60 )
        return res
    return wrapper

@print_timing                                                                      |
def foo():                                                                         |
    return 'foo' 

class name(object):
       def __init__(self, name):
              self.name = name
       @print_timing
       def __call__(self):
              return self.name

bar = name("bar")
print bar()

This returns:

__call__ took 0.000 s ~ 0 min and 0.0 sec
bar

The object bar behaves like a function called bar, but exposes the internal implementation detail of __call__ when used with the decorator print_timing. Is there a way to change the name object (perhaps by passing a suitable argument to the __init__ function) so it returns instead

 bar took 0.000 s ~ 0 min and 0.0 sec

? I want a solution that will let the print_timing decorator continue to work with ordinary functions. Running
print foo() gives

foo took 0.000 s ~ 0 min and 0.0 sec
foo
  • 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-22T16:44:35+00:00Added an answer on May 22, 2026 at 4:44 pm

    As long as you use the decorator only on methods, they’ll be passed self as the first argument:

    def print_timing(func):
        import time
        def wrapper(*args, **kwargs):
            t1 = time.time()
            res = func(*args, **kwargs)
            t2 = time.time()
            funcname = func.__name__
            # Special case; a "name" instance has a "name" attribute we want to use instead.
            if len(args) >= 1 and isinstance(args[0], name):
                funcname = args[0].name
            print '%s took %0.3f s ~ %0.0f min and %0.1f sec' % (funcname, t2-t1, int(t2 - t1)/60, (t2-t1) % 60 )
            return res
        return wrapper
    

    Updated: The wrapper now uses func.__name__ by default, but if you use this on a name class (as in your original question), it’ll use the name attribute of the instance instead.

    I’ve used an isinstance test to determine that a name attribute will be present, but you could use duck-typing instead (if hasattr(args[0], 'name')); the name variable is so generic however that you most likely will get unexpected results when used on arbitrary class methods.

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

Sidebar

Related Questions

Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
Consider the following code fragment in VS2010 Beta 1: let array = Array2D.zeroCreate 1000
Consider the following code fragment: let t1 = async { return process1() } let
Consider the following code: class Program { static void Main(string[] args) { try {
Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set
Consider the following code fragment: int (*p)[3]; int (*q)[3]; q = p; q++; printf(%d,
Consider the following two fragments of code: scala> def f1(x:Any) = x match {
Consider the following code fragment: for(/* some condition */) { int x = rand();
Consider the following code: Here is my price calculator controller header file. #import <Foundation/Foundation.h>
Consider the following code: from twisted.internet.defer import Deferred d1 = Deferred() d2 = Deferred()

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.