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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:27:43+00:00 2026-05-26T02:27:43+00:00

My issue is that I am using a metaclass to wrap certain class methods

  • 0

My issue is that I am using a metaclass to wrap certain class methods in a timer for logging purposes.

For example:

class MyMeta(type):

    @staticmethod
    def time_method(method):
        def __wrapper(self, *args, **kwargs):
            start = time.time()
            result = method(self, *args, **kwargs)
            finish = time.time()
            sys.stdout.write('instancemethod %s took %0.3f s.\n' %(
                method.__name__, (finish - start)))
            return result
        return __wrapper

    def __new__(cls, name, bases, attrs):
        for attr in ['__init__', 'run']:
            if not attr in attrs:
                continue

            attrs[attr] = cls.time_method(attrs[attr])

        return super(MetaBuilderModule, cls).__new__(cls, name, bases, attrs)

The problem I’m having is that my wrapper runs for every ‘__init__’ even though I really only want it for the current module I am instantiating. The same goes for any method want to time. I dont want the timing to run on any inherited methods UNLESS they aren’t being overridden.

class MyClass0(object):
    __metaclass__ = MyMeta
    def __init__(self):
        pass

    def run(self): 
        sys.stdout.write('running')
        return True


class MyClass1(MyClass0):
    def __init__(self): # I want this timed
        MyClass0.__init__(self) # But not this.
        pass

    ''' I need the inherited 'run' to be timed. '''

I’ve tried a few things but so far I’ve had no success.

  • 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-26T02:27:44+00:00Added an answer on May 26, 2026 at 2:27 am

    Guard the timing code with an attribute. That way, only the outermost decorated method on an object will actually get timed.

    @staticmethod
    def time_method(method):
        def __wrapper(self, *args, **kwargs):
            if hasattr(self, '_being_timed'):
                # We're being timed already; just run the method
                return method(self, *args, **kwargs)
            else:
                # Not timed yet; run the timing code
                self._being_timed = True  # remember we're being timed
                try:
                    start = time.time()
                    result = method(self, *args, **kwargs)
                    finish = time.time()
                    sys.stdout.write('instancemethod %s took %0.3f s.\n' %(
                        method.__name__, (finish - start)))
                    return result
                finally:
                    # Done timing, reset to original state
                    del self._being_timed
        return __wrapper
    

    Timing only the outermost method is slightly different than “not timing inherited methods unless they aren’t being overridden”, but I believe it solves your problem.

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

Sidebar

Related Questions

I have an issue that is driving me a bit nuts: Using a UserProfileManager
Ok, I understand that using strings that have special characters is an encoding issue.
Our issue is that our project has files being downloaded using wget to the
An interesting issue came up recently. We came across some code that is using
I have an odd issue while using FlashCS4. I have a textfield that, when
We're using SQL Server 2000 Query Analyser, and one issue we have is that
I have this issue that appears only when using a webkit browser. Sometimes, when
I have a stored procedure that I am calling using EXECUTE IMMEDIATE. The issue
I am using Telerik asp.net mvc extensions. I have an issue that happens only
I have a rather annoying issue that I solved using a simple recursive method

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.