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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:20:00+00:00 2026-06-09T23:20:00+00:00

Slightly modifying the answer from Applying python decorators to methods in a class ,

  • 0

Slightly modifying the answer from Applying python decorators to methods in a class, it is possible to apply a decorator to every method in a class. Is there any way to do this without the inspect module? I’ve been trying to accomplish this using metaclasses and modifying __getattribute__ but I keep getting infinite recursion. From How is the __getattribute__ method used?, this can be fixed in normal classes using object.__getattribute__(self, name). Is there anything equivalent for metaclasses?

  • 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-09T23:20:02+00:00Added an answer on June 9, 2026 at 11:20 pm

    Define a meta class and then just apply decorator at the end of class definition.

    class Classname:
       def foo(self): pass
    
    for name, fn in inspect.getmembers(Classname):
        if isinstance(fn, types.UnboundMethodType):
            setattr(Classname, name, decorator(fn))
    

    For Python 3 just replace the types.UnboundMethodType with types.FunctionType.

    but if you really don;t wanna use inspect than you can do it like this

    import types
    
    class DecoMeta(type):
       def __new__(cls, name, bases, attrs):
    
          for attr_name, attr_value in attrs.iteritems():
             if isinstance(attr_value, types.FunctionType):
                attrs[attr_name] = cls.deco(attr_value)
    
          return super(DecoMeta, cls).__new__(cls, name, bases, attrs)
    
       @classmethod
       def deco(cls, func):
          def wrapper(*args, **kwargs):
             print "before",func.func_name
             func(*args, **kwargs)
             print "after",func.func_name
          return wrapper
    
    class MyKlass(object):
       __metaclass__ = DecoMeta
    
       def func1(self): 
          pass
    
    MyKlass().func1()
    

    Output:

    before func1
    after func1

    Note: it will not decorate staticmethod and classmethod

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

Sidebar

Related Questions

I slightly remember that there is an class which is capable of stretching an
Slightly possible duplicate : Instance method vs. static method with ref parameter If I
Borrowing from the example on the Matplotlib documentation page and slightly modifying the code,
I am modifying a sql server 2005 stored procedure slightly for performance, and I
Slightly daft, but... Is there a way to prevent Visual Studio treating a .jpg
I am modifying a wordpress template and need to slightly separate rendering logic for
I'm going to use the sample code from http://gettinggeneticsdone.blogspot.com/2009/11/split-apply-and-combine-in-r-using-plyr.html for this example. So, first,
Im slightly confused by a method in the UIGestureRecognizerDelegate protocol. When I implement the
Im slightly going out of my mind trying to get 3 result sets from
slightly different question about variance this time. I take it from experimentation that C#

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.