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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:28:03+00:00 2026-05-20T09:28:03+00:00

In order to set metaclass of a class, we use the __metaclass__ attribute. Metaclasses

  • 0

In order to set metaclass of a class, we use the __metaclass__ attribute. Metaclasses are used at the time the class is defined, so setting it explicitly after the class definition has no effect.

This is what happens when I try to set metaclasses explicitly;

>>> class MetaClass(type):
    def __new__(cls, name, bases, dct):
        dct["test_var"]=True
        return type.__new__(cls, name, bases, dct)
    def __init__(cls, name, bases, dct):
        super(MetaClass, cls).__init__(name, bases, dct)


>>> class A:
    __metaclass__=MetaClass


>>> A.test_var
True
>>> class B:
    pass

>>> B.__metaclass__=MetaClass
>>> B.test_var

Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    B.test_var
AttributeError: class B has no attribute 'test_var'

The best idea I can think of is to re-define whole class and add the __metaclass__ attribute dynamically somehow. Or do you know a better way set metaclass after the class definition?

  • 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-20T09:28:04+00:00Added an answer on May 20, 2026 at 9:28 am

    You can change the metaclass after class creation the same way that you can change the class of an object, however you’d have a lot of issues. For starters, the initial metaclass needs to be different from type, the __init__ and __new__ of the new metaclass won’t be called (though you can manually call __init__ or a method that performs __init__‘s job).

    Possibly the least troublesome way to change the metaclass is to recreate the class again from scratch:

    B = MetaClass(B.__name__, B.__bases__, B.__dict__)
    

    But if you insist on changing the metaclass dynamically, you first need to define B with a temporary custom metaclass:

    class _TempMetaclass(type):
        pass
    
    class B:
        __metaclass__ = _TempMetaclass # or: type('temp', (type, ), {})
    

    Then you can define the metaclass like that:

    class MetaClass(type):
        def __init__(cls, *a, **kw):
            super(MetaClass, cls).__init__(*a, **kw)
            cls._actual_init(*a, **kw)
        def _actual_init(cls, *a, **kw):
            # actual initialization goes here
    

    And then do something like:

    B.__class__ = MetaClass
    MetaClass._actual_init(B, B.__name__, B.__bases__, B.__dict__)
    

    You also need to make sure that all the initialization of the class is done _actual_init. You can also add a classmethod to the metaclass that changes the metaclass for you.

    Both solutions have the slight shortcoming that B’s bases would be limited – they need to be compatible with both the original and the new metaclass, but I guess that’s not an issue in your case.

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

Sidebar

Related Questions

I want to target specific iframes using a class attribute in order to set
I'm trying to override Backbone.sync in order to set the id attribute every time
Is there any way to set order by clause in HQL by run time.
I tried to use the following function in order to set the div's position
I have excluded a field like this in order to set it's label. class
I use ADO.NET from C# 4 in order to set up a test context
I would use only a single query: $sql = UPDATE gallery SET order =
I defined multiples rules in one DRL file, how to set order, want to
So I have this switch in order to set a wait time public int
Is it possible to explicitly control the culture of controls in order to set

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.