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

  • Home
  • SEARCH
  • 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 588239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:17:51+00:00 2026-05-13T15:17:51+00:00

I have been trying to understand python metaclasses, and so have been going through

  • 0

I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass like

def metacls(clsName, bases, atts):
    ....
    return type(clsName, bases, atts)

However, I have seen a lot of people write their metaclasses in the following way:

class Metacls(type):
    def __new__(meta, clsName, bases, atts):
        ....
        return type.__new__(meta, clsName, bases, atts)

As far as I can see, these would both do the same thing. Is there any reason to use the base class instead? Is it customary?

  • 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-13T15:17:52+00:00Added an answer on May 13, 2026 at 3:17 pm

    There are subtle differences, mostly relating to inheritance. When using a
    function as a metaclass, the resulting class is really an instance of type,
    and can be inherited from without restriction; however, the metaclass function
    will never be called for such subclasses. When using a subclass of type as a
    metaclass, the resulting class will be an instance of that metaclass, as will
    any of its subclasses; however, multiple inheritance will be restricted.

    Illustrating the differences:

    >>> def m1(name, bases, atts):
    >>>     print "m1 called for " + name
    >>>     return type(name, bases, atts)
    >>>
    
    >>> def m2(name, bases, atts):
    >>>     print "m2 called for " + name
    >>>     return type(name, bases, atts)
    >>>
    
    >>> class c1(object):
    >>>     __metaclass__ = m1
    m1 called for c1
    
    >>> type(c1)
    <type 'type'>
    
    >>> class sub1(c1):
    >>>     pass
    
    >>> type(sub1)
    <type 'type'>
    
    >>> class c2(object):
    >>>     __metaclass__ = m2
    m2 called for c2
    
    >>> class sub2(c1, c2):
    >>>     pass
    
    >>> type(sub2)
    <type 'type'>
    

    Note that when defining sub1 and sub2, no metaclass functions were called.
    They will be created exactly as if c1 and c2 had no metaclasses, but instead
    had been manipulated after creation.

    >>> class M1(type):
    >>>     def __new__(meta, name, bases, atts):
    >>>         print "M1 called for " + name
    >>>         return super(M1, meta).__new__(meta, name, bases, atts)
    
    >>> class C1(object):
    >>>     __metaclass__ = M1
    M1 called for C1
    
    >>> type(C1)
    <class '__main__.M1'>
    
    >>> class Sub1(C1):
    >>>     pass
    M1 called for Sub1
    
    >>> type(Sub1)
    <class '__main__.M1'>
    

    Note the differences already: M1 was called when creating Sub1, and both
    classes are instances of M1. I’m using super() for the actual creation here,
    for reasons which will become clear later.

    >>> class M2(type):
    >>>     def __new__(meta, name, bases, atts):
    >>>         print "M2 called for " + name
    >>>         return super(M2, meta).__new__(meta, name, bases, atts)
    
    >>> class C2(object):
    >>>     __metaclass__ = M2
    M2 called for C2
    
    >>> type(C2)
    <class '__main__.M2'>
    
    >>> class Sub2(C1, C2):
    >>>     pass
    M1 called for Sub2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 23, in __new__
    TypeError: Error when calling the metaclass bases
        metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
    

    This is the major restriction on multiple inheritance with metaclasses.
    Python doesn’t know whether M1 and M2 are compatible metaclasses,
    so it forces you to create a new one to guarantee that it does what you need.

    >>> class M3(M1, M2):
    >>>     def __new__(meta, name, bases, atts):
    >>>         print "M3 called for " + name
    >>>         return super(M3, meta).__new__(meta, name, bases, atts)
    
    >>> class C3(C1, C2):
    >>>     __metaclass__ = M3
    M3 called for C3
    M1 called for C3
    M2 called for C3
    
    >>> type(C3)
    <class '__main__.M3'>
    

    This is why I used super() in the metaclass __new__ functions: so each one
    can call the next one in the MRO.

    Certain use cases might need your classes to be of type type, or might want
    to avoid the inheritance issues, in which case a metaclass function is probably
    the way to go. In other cases, the type of the class might be truly important,
    or you might want to operate on all subclasses, in which case subclassing
    type would be a better idea. Feel free to use the style that fits best in
    any given situation.

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

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would just do a magnitude above the highest value,… May 14, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer Of course it does not work - you are redirecting… May 14, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer Searchlogic can be combined with existing named scopes and must… May 14, 2026 at 8:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.