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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:29:43+00:00 2026-06-01T05:29:43+00:00

What is a simple example of decorating a class by defining the decorator as

  • 0

What is a simple example of decorating a class by defining the decorator as a class?

I’m trying to achieve what has been implemented in Python 2.6 using PEP 3129 except using classes not functions as Bruce Eckel explains here.

The following works:

class Decorator(object):
    def __init__(self, arg):
        self.arg = arg

    def __call__(self, cls):
        def wrappedClass(*args):
            return cls(*args)
        return type("TestClass", (cls,), dict(newMethod=self.newMethod, classattr=self.arg))

    def newMethod(self, value):
        return value * 2

@Decorator("decorated class")
class TestClass(object):
    def __init__(self):
        self.name = "TestClass"
        print "init %s"%self.name

    def TestMethodInTestClass(self):
        print "test method in test class"

    def newMethod(self, value):
        return value * 3

Except, in the above, wrappedClass is not a class, but a function manipulated to return a class type. I would like to write the same callable as follows:

def __call__(self, cls):
        class wrappedClass(cls):
            def __init__(self):
                ... some code here ...
        return wrappedClass

How would this be done?

I’m not entirely sure what goes into “””… some code here …”””

  • 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-01T05:29:44+00:00Added an answer on June 1, 2026 at 5:29 am

    If you want to overwrite new_method(), just do it:

    class Decorator(object):
        def __init__(self, arg):
            self.arg = arg
        def __call__(self, cls):
            class Wrapped(cls):
                classattr = self.arg
                def new_method(self, value):
                    return value * 2
            return Wrapped
    
    @Decorator("decorated class")
    class TestClass(object):
        def new_method(self, value):
            return value * 3
    

    If you don’t want to alter __init__(), you don’t need to overwrite it.

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

Sidebar

Related Questions

Simple example of scenario I am trying to implement class ATest { int a;
Can someone share a simple example of using the foreach keyword with custom objects?
I'm trying to display a modal form using jquery ui. I've adapted an example
I'm trying to set up a simple way of decorating methods in my CherryPy
Below is a simple example using gtk2hs that adds a label and then a
Simple example: public class Person { String name; } public class VIP extends Person
Simple example using boost::phoenix: #include <vector> #include <algorithm> #include <boost/phoenix.hpp> namespace ph = boost::phoenix;
Here is a very simple example. public interface IMyInterfaceProperty { } public class MyProperty
Simple example: $('foo').bind('doubletap swiperight', function() {}); So, I am trying to be really efficient.
Simple example: template <class P> class MyT { struct Item { public: Item() {}

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.