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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:21:25+00:00 2026-05-23T13:21:25+00:00

After crashing with metaclasses i delved into the topic of metaprogramming in Python and

  • 0

After crashing with metaclasses i delved into the topic of metaprogramming in Python and I have a couple of questions that are, imho, not clearly anwered in available docs.

  1. When using both __new__ and __init__ in a metaclass, their arguments must be defined the same?
  2. What’s most efficient way to define class __init__ in a metaclass?
  3. Is there any way to refer to class instance (normally self) in a metaclass?
  • 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-23T13:21:25+00:00Added an answer on May 23, 2026 at 1:21 pm

    For 1: The __init__ and __new__ of any class have to accept the same arguments, because they would be called with the same arguments. It’s common for __new__ to take more arguments that it ignores (e.g. object.__new__ takes any arguments and it ignores them) so that __new__ doesn’t have to be overridden during inheritance, but you usually only do that when you have no __new__ at all.

    This isn’t a problem here, because as it was stated, metaclasses are always called with the same set of arguments always so you can’t run into trouble. With the arguments at least. But if you’re modifying the arguments that are passed to the parent class, you need to modify them in both.

    For 2: You usually don’t define the class __init__ in a metaclass. You can write a wrapper and replace the __init__ of the class in either __new__ or __init__ of the metaclass, or you can redefine the __call__ on the metaclass. The former would act weirdly if you use inheritance.

    import functools
    
    class A(type):
        def __call__(cls, *args, **kwargs):
            r = super(A, cls).__call__(*args, **kwargs)
            print "%s was instantiated" % (cls.__name__, )
            print "the new instance is %r" % (r, )
            return r
    
    
    class B(type):
        def __init__(cls, name, bases, dct):
            super(B, cls).__init__(name, bases, dct)
            if '__init__' not in dct:
                return
            old_init = dct['__init__']
            @functools.wraps(old_init)
            def __init__(self, *args, **kwargs):
                old_init(self, *args, **kwargs)
                print "%s (%s) was instantiated" % (type(self).__name__, cls.__name__)
                print "the new instance is %r" % (self, )
            cls.__init__ = __init__
    
    
    class T1:
        __metaclass__ = A
    
    class T2:
        __metaclass__ = B
        def __init__(self): 
            pass
    
    class T3(T2):
        def __init__(self):
            super(T3, self).__init__()
    

    And the result from calling it:

    >>> T1()
    T1 was instantiated
    the new instance is <__main__.T1 object at 0x7f502c104290>
    <__main__.T1 object at 0x7f502c104290>
    >>> T2()
    T2 (T2) was instantiated
    the new instance is <__main__.T2 object at 0x7f502c0f7ed0>
    <__main__.T2 object at 0x7f502c0f7ed0>
    >>> T3()
    T3 (T2) was instantiated
    the new instance is <__main__.T3 object at 0x7f502c104290>
    T3 (T3) was instantiated
    the new instance is <__main__.T3 object at 0x7f502c104290>
    <__main__.T3 object at 0x7f502c104290>
    

    For 3: Yes, from __call__ as shown above.

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

Sidebar

Related Questions

We have a Windows service written in C#. it is crashing after running for
Hey i have a UserControl that kept crashing my Visual Studio. So i ran
After being told by at least 10 people on SO that version control was
After lots of attempts and search I have never found a satisfactory way to
After hours of debugging, it appears to me that in FireFox, the innerHTML of
After I was convinced that labeled breaks/continues are a total nono over here ,
I am having problems with Eclipse (3.5.x Galileo) crashing after installing ADT 0.9.7 ,
I have an AppPool which keeps crashing, initially with the below errors in the
I'm gettin the followig error after converting JSON echo into a dictionary, * Terminating
I have solution with few projects for calculation proposes. Code after 20 minutes of

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.