In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses.
Questions tagged [metaclass]
1179 questions
28
votes
1 answer
Dynamic Class Creation in SQLAlchemy
We have a need to create SQLAlchemy classes to access multiple external data sources that will increase in number over time. We use the declarative base for our core ORM models and I know we can manually specify new ORM classes using the…

PlaidFan
- 797
- 11
- 20
25
votes
1 answer
Are classes objects in Objective-C?
okay, so I understand that an object is an instance of a class that must be allocated and initialized, but are classes themselves objects?
I know when you create a new class it is an instance of something else, like NSObject. So, if this makes it a…

jerry
- 2,743
- 9
- 41
- 61
25
votes
1 answer
Inheritance of metaclass
In this well known answer that explains metaclass in Python. It mentions that the __metaclass__ attribute will not be inherited.
But as a matter of fact, I tried in Python:
class Meta1(type):
def __new__(cls, clsname, bases, dct):
print…

Lifu Huang
- 11,930
- 14
- 55
- 77
25
votes
4 answers
Why doesn't the namedtuple module use a metaclass to create nt class objects?
I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string.…

Rick
- 43,029
- 15
- 76
- 119
24
votes
3 answers
How can I create my own "parameterized" type in Python (like `Optional[T]`)?
I want to create my own parameterized type in Python for use in type hinting:
class MaybeWrapped:
# magic goes here
T = TypeVar('T')
assert MaybeWrapped[T] == Union[T, Tuple[T]]
Never mind the contrived example; how can I implement this? I…

shadowtalker
- 12,529
- 3
- 53
- 96
24
votes
1 answer
How to determine the metaclass of a class?
I have a class object, cls. I want to know its metaclass. How do I do this?
(If I wanted to know its parent classes, I would do cls.__mro__. Is there something like this to get the metaclass?)

Pro Q
- 4,391
- 4
- 43
- 92
24
votes
2 answers
How to add properties to a metaclass instance?
I would like to define metaclass that will enable me to create properties (i.e. setter, getter) in new class on the base of the class's attributes.
For example, I would like to define the class:
class Person(metaclass=MetaReadOnly):
name =…

hakubaa
- 305
- 2
- 7
22
votes
1 answer
What's the usage of a virtual subclass?
class AnimalMeta(type):
def __instancecheck__(cls, instance):
return cls.__subclasscheck__(type(instance))
def __subclasscheck__(cls, sub):
return (hasattr(sub, 'eat') and callable(sub.eat) and
hasattr(sub,…

weiwang
- 393
- 3
- 10
22
votes
4 answers
1 class inherits 2 different metaclasses (abcmeta and user defined meta)
I have a class1 that needs to inherit from 2 different metaclasses which is Meta1 and abc.ABCMeta
Current implementation:
Implementation of Meta1:
class Meta1(type):
def __new__(cls, classname, parent, attr):
new_class =…

momokjaaaaa
- 1,293
- 3
- 17
- 32
22
votes
6 answers
What is the difference between type and type.__new__ in python?
I was writing a metaclass and accidentally did it like this:
class MetaCls(type):
def __new__(cls, name, bases, dict):
return type(name, bases, dict)
...instead of like this:
class MetaCls(type):
def __new__(cls, name, bases,…

Jason Baker
- 192,085
- 135
- 376
- 510
22
votes
2 answers
Sphinx autodoc gives WARNING: py:class reference target not found: type warning
I've got some code that uses a metaclass in python. But when sphinx autodoc is run it is giving the error:
WARNING: py:class reference target not found: type
The error is occuring in a line of an auto generated .rst file:
.. automodule::…

Douglas Schneider
- 221
- 2
- 4
21
votes
5 answers
Is there a way to set metaclass after the class definition?
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…

Utku Zihnioglu
- 4,714
- 3
- 38
- 50
21
votes
1 answer
Why should I use the __prepare__ method to get a class' namespace?
Note This question is not about the Python 3 Enum data type, it's just the example I'm using.
With PEP 3115 Python 3 added the __prepare__1 method to type for the purpose of allowing a custom namespace to be used when creating classes. For example,…

Ethan Furman
- 63,992
- 20
- 159
- 237
21
votes
1 answer
When should I subclass EnumMeta instead of Enum?
In this article Nick Coghlan talks about some of the design decisions that went in to the PEP 435 Enum type, and how EnumMeta can be subclassed to provide a different Enum experience.
However, the advice I give (and I am the primary stdlib Enum…

Ethan Furman
- 63,992
- 20
- 159
- 237
21
votes
1 answer
Provide __classcell__ example for Python 3.6 metaclass
Per the 3.6.0 docs:
CPython implementation detail: In CPython 3.6 and later, the __class__
cell is passed to the metaclass as a __classcell__ entry in the class
namespace. If present, this must be propagated up to the type.__new__
call in…

Dima Tisnek
- 11,241
- 4
- 68
- 120