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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:50:03+00:00 2026-05-30T16:50:03+00:00

class MType(type): pass class MClass(object): __metaclass__ = MType a = MType From the above

  • 0
class MType(type):
 pass

class MClass(object):
 __metaclass__ = MType

a = MType

From the above snippet is there anyway I can create an instance of MClass given a?

(It’s probably important to note that I won’t be able to simply contruct MClass())

  • 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-30T16:50:04+00:00Added an answer on May 30, 2026 at 4:50 pm

    There are two straightforward ways I can think of to find the instances of a metaclass. One is to have the metaclass keep track of a list of its instances as they are created, as follows:

    class MType(type):
        instances = []    #list of instances of the metaclass
    
        def __init__(cls, name, bases, dct):
                MType.instances.append(cls)    #append to list of instances
                super(MType, cls).__init__(name, bases, dct)
    
    class MClass(object):
        __metaclass__ = MType
    

    Now MType.instances is [<class '__main__.MClass'>].

    Another way is to use the
    inspect module to look through the classes in, say, a given module, and use isinstance to check if they are instances of MType. This isn’t necessarily a crazy thing to do, either – for example, various unit testing suites work by looking through a module for classes and functions that appear to be tests, based on their names and/or inherited classes – I’m not really sure why you would want to do this with a metaclass, though.

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

Sidebar

Related Questions

class ClassA: pass mytype = type(ClassA) Using mytype , how do you create an
Is there a way in .NET to create the source code class definition given
To get the type of a class called myClass I do this Type myType
class MyBase { protected object PropertyOfBase { get; set; } } class MyType :
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
Why is this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class
There's a lot one can find about this googling a bit but I haven't
So I have a class which overrides Equals(object obj) and GetHashCode() along with implementing
The following code import types class A: class D: pass class C: pass for
I'm trying to serialize a Type object in the following way: Type myType =

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.