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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:39:09+00:00 2026-06-13T01:39:09+00:00

I would like to provide a decorator that allows for an optional configuration when

  • 0

I would like to provide a decorator that allows for an optional configuration when applied to a function.

A simple implementation follows:

import functools


class Deco(object):
    config = {'message': 'hello'}

    def __init__(self, func):
         self.func = func
         functools.wraps(func)(self)

    def __call__(self, *args, **kwargs):
         print self.config['message']
         return self.func(*args, **kwargs)

    @classmethod
    def customize(cls, **kwargs):
         """Return a customized instance of this class. """
         return type(cls.__name__, (Deco, ), {'config': kwargs})


@Deco
def add(a, b): 
    return a + b 


@Deco.customize(message='bye')
def sub(a, b): 
    return a - b 


>>> add(1, 2)
'hello'
>>> sub(2, 1)
'bye'

I would like to use it to provide user-friendly decorators for Django views.

This approach works without errors, but is there something bad about allowing a class to have a static factory method instantiating customized instances of it self, as a decorator?

  • 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-13T01:39:10+00:00Added an answer on June 13, 2026 at 1:39 am

    You could work without creating an extra sub-class for each time the decorator is used there, but your code is fine. The way without extra subclass could be something along:

    class Deco(object):
        config = {'message': 'hello'}
    
        def __init__(self, func=None, **kwargs):
             if kwargs:
                 self.config = kwargs
             if func is not None:
                 self._decorate(func)
    
        def _decorate(self, func):
            self.func = func
            functools.wraps(func)(self)
    
        def __call__(self, *args, **kwargs):
            if not hasattr(self, "func"):
                self._decorate(func)
                return self
            print self.config['message']
            return self.func(*args, **kwargs)
    

    So, while performance wise there would be no difference to your code (unless you would be decorating at least hundreds of thousands of functions – your code create an extra object – a class – for each time the decorator is used, besides the instance of that class) – there is an impact on people would review your code (either to use your modules, or to maintain it after you are done). I mean “a decorator that dynamically generates subclasses of itself” may sound too advanced and scare people away. Although it is as simple as my suggestion above once one understands the mechanisms of class generation in Python as you had.

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

Sidebar

Related Questions

I would like to provide a templated function, that varies its implementation (->specialization) according
I would like to provide a PHP hanging request that will stay open until
I would like to know how to provide a formatting capabilities that enable the
I would like to provide an anonymous access to EJB3 Session Bean. So that
I would like to provide database for my program that uses elixir for ORM.
I would like to provide a web tool that requires access to a large
I would like to provide my own implementation of sorting behaviour for the WPF
I would like to provide a deafult implementation for an event generated by an
I would like to provide my client a native iPhone application module that he
I would like to implement a decorator that provides per-request caching to any 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.