-1
cls = type("Test", (Base,), {"__metaclass__": Meta, "a": 1, ...})

I want to make some check for the attrs in the 3rd argument with Meta class, but this seem not worked, is there any other method?

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
wartalker
  • 338
  • 4
  • 11

2 Answers2

4

Meta-class is a subclass of type. So creating a type of a metaclass is calling the meta class:

cls = Meta("Test", (Base,), {"a": 1, ...})
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

You should create your Test class by calling the Meta (a.k.a. type.__class__.__new__(Meta, "Test", (Base,), {"__metaclass__": Meta, "a": 1}).

If you have a dictionary with possible __metaclass__, you can use the following code:

members = {"__metaclass__": Meta, "a": 1, ...}  
metaclass = members.pop('__metaclass__', type)
cls = metaclass("Test", (Base,), members)