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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:44:22+00:00 2026-05-17T18:44:22+00:00

I am trying to decorate an actual class, using this code: def my_decorator(cls): def

  • 0

I am trying to decorate an actual class, using this code:

def my_decorator(cls):
    def wrap(*args, **kw):
        return object.__new__(cls)
    return wrap

@my_decorator
class TestClass(object):
    def __init__(self):
        print "__init__ should run if object.__new__ correctly returns an instance of cls"


test = TestClass() # shouldn't TestClass.__init__() be run here?

I get no errors, but I also don’t see the message from TestClass.__init__().

According to the docs for new-style classes:

Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

Any ideas why __init__ isn’t running?

Also, I have tried to call __new__ like this:

return super(cls.__bases__[0], cls).__new__(cls)

but it would return a TypeError:

TypeError: super.__new__(TestClass): TestClass is not a subtype of super
  • 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-17T18:44:22+00:00Added an answer on May 17, 2026 at 6:44 pm

    __init__ isn’t running because object.__new__ doesn’t know to call it. If you change it to
    cls.__call__(*args, **kwargs), or better, cls(*args, **kwargs), it should work. Remember that a class is a callable: calling it produces a new instance. Just calling __new__ returns an instance but doesn’t go through the initialization. An alternative would be to call __new__ and then manually call __init__ but this is just replacing the logic that is already embodied in __call__.

    The documentation that you quote is referring to calling super from within the __new__ method of the class. Here, you are calling it from the outside and not in the usual way as I’ve already discussed.

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

Sidebar

Related Questions

I'm trying to achieve the following: class Data(object): def __init__(self, data): self.data = data
I am trying to understand the code for the mixins posted at this blog
So I am trying to simply decorate a class to serialize it as XML.
I'm trying to validate a class decorated with data annotation with the Validator class
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
I am trying to decorate a Django view by two decorators, one for checking
I am trying to decorate the magic method __getitem__ to be a classmethod on
I am trying to create a custom control which does nothing except decorate the

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.