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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:12:22+00:00 2026-05-27T00:12:22+00:00

I am working on finding a way to reduce boilerplate decorators. We have a

  • 0

I am working on finding a way to reduce boilerplate decorators. We have a lot of classes that use a @decorate. For example:

class MyClass(Base):
     @decorate
     def fun1(self):
         pass
     @decorate
     def fun2(self):
         pass
     def fun3(self):
         pass

I want to make it so by default the decorator is there, unless someone specifies otherwise.


I use this code to do the autowrap

from functools import wraps

def myDecorator(func):
    @wraps(func)
    def decorator(self, *args, **kwargs):
        try:
            print 'enter'
            ret = func(self, *args, **kwargs)
            print 'leave'
        except:
            print 'exception'
            ret = None

        return ret

    return decorator

class TestDecorateAllMeta(type):
    def __new__(cls, name, bases, local):
        for attr in local:
            value = local[attr]
            if callable(value):
                local[attr] = myDecorator(value)
        return type.__new__(cls, name, bases, local)

class TestClass(object):
    __metaclass__ = TestDecorateAllMeta

    def test_print2(self, val):
        print val

    def test_print(self, val):
        print val

c = TestClass()
c.test_print1("print 1")
c.test_print2("print 2")

My question are:

  1. Is there a better way to accompish auto-decorating?
  2. How can I go about overriding?

Ideally my end solution would be something like:

class TestClass(object):
    __metaclass__ = TestDecorateAllMeta

    def autowrap(self):
        print("Auto wrap")

    @dont_decorate
    def test_dont_decorate(self, val):
        print val

Edit

To speak to one of the comments below, since classess are callable instead of doing

if callable(value):

It should read:

if isinstance(value,types.FunctionType) 
  • 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-27T00:12:23+00:00Added an answer on May 27, 2026 at 12:12 am

    Rather than making the user of my class specify a __metaclass__ attribute I would just have them derive from my base class that defines it. No need to expose the plumbing unnecessarily.

    Other than that, looks good, though. Your @dont_decorate function decorator can be implemented by setting an attribute on the original function, which your class decorator then detects and skips the decoration if it is present.

    def dont_decorate(func):
        func._dont_decorate = True
        return func
    

    Then in your metaclass, where you now have the line if callable(value): just put:

    if callable(value) and not hasttr(value, "_dont_decorate"):
    

    As an aside, classes are callable so if you don’t want inner classes decorated, you should probably check for functions using isinstance() rather than callable().

    If you are interested in more explicit alternatives, you might take a look at this recent question where someone wanted to do essentially the same thing using a class decorator. Unfortunately, this is a good bit more complicated because the methods are already wrapped by the time the decorator sees them.

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

Sidebar

Related Questions

I am working on finding a good way to make user submitted data, in
Presently Iam working in finding disparity of stereo pair. I have got a situation
Working on a bunch of forms at the moment and I'm finding that I
I've learned the hard way that regexes cannot adequately parse html, prior to finding
Essentially its a pacman clone game I'm working on. I have an Enemy class,
Does anyone have a good way of finding if a string contains malformed XHTML
I'm working with an xml fragment, and finding that I'm doing the following a
I have been recently working on an easy to use Syntax Highlighting system for
I am working on a new project that needs to use Linq To SQL
I'm finding it challenging to get data-binding working for my sample WPF application in

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.