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
13
votes
2 answers
python metaclasses at module level
I read What is a metaclass in Python?
and I tried to replicate the upper metaclass from the example and found that this doesn't work in all cases:
def upper(cls_name, cls_parents, cls_attr):
""" Make all…

Alex Plugaru
- 2,209
- 19
- 26
13
votes
1 answer
python 3.2 plugin factory: instantiation from class/metaclass
I'm riffing from the information here: Metaclass not being called in subclasses
My problem is that I'm unable to create an instance of an object using this class registry. If I use "regular" construction methods, then it seems to instantiate…

drone115b
- 208
- 1
- 6
13
votes
3 answers
Architecture for providing different linear algebra back-ends
I am prototyping a new system in Python; the functionality is mostly numerical.
An important requirement is the ability to use different linear algebra back-ends: from individual user implementations to generic libraries, such as Numpy. The linear…

Escualo
- 40,844
- 23
- 87
- 135
13
votes
1 answer
Setting a class __name__ declaratively
Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier?
>>> class Potato:
... __name__ = 'not Potato'
...
>>> Potato.__name__ # doesn't stick
'Potato'
>>> Potato().__name__ # .. but…

wim
- 338,267
- 99
- 616
- 750
13
votes
1 answer
How does isinstance work for List?
I'm trying to understand how Python's type annotations work (e.g. List and Dict - not list or dict). Specifically I'm interested in how isinstance(list(), List) works, so that I can create my own custom annotations.
I see that List is defined…

c z
- 7,726
- 3
- 46
- 59
13
votes
2 answers
python abstractmethod with another baseclass breaks abstract functionality
Consider the following code example
import abc
class ABCtest(abc.ABC):
@abc.abstractmethod
def foo(self):
raise RuntimeError("Abstract method was called, this should be impossible")
class ABCtest_B(ABCtest):
pass
test =…

IARI
- 1,217
- 1
- 18
- 35
13
votes
4 answers
methods of metaclasses on class instances
I was wondering what happens to methods declared on a metaclass. I expected that if you declare a method on a metaclass, it will end up being a classmethod, however, the behavior is different. Example
>>> class A(object):
... @classmethod
... …

Stefano Borini
- 138,652
- 96
- 297
- 431
13
votes
1 answer
Metaclass not being called in subclasses
Here is a python session.
>>> class Z(type):
def __new__(cls, name, bases, attrs):
print cls
print name
return type(name, bases, attrs)
...
>>> class Y(object):
__metaclass__ = Z
...

agiliq
- 7,518
- 14
- 54
- 74
12
votes
4 answers
Removing specific methods from child class which are inherited from parent class
The code is as below, just the basic structure:
class FooType(type):
def __new__( cls, name, bases, classdict ):
instance = type.__new__( cls, name, bases, classdict )
# What can I do here?
return instance
class FooBase(…

skulled
- 133
- 1
- 6
12
votes
1 answer
How different is type.__setattr__ from object.__setattr__?
type.__setattr__ is used for classes, basically instances of metaclasses. object.__setattr__ on the other hand, is used for instances of classes. This is totally understood.
I don't see a significant difference between the two method, at least at…

GIZ
- 4,409
- 1
- 24
- 43
12
votes
0 answers
What is the difference between Abstract Classes and Metaclasses in python?
I read two articles about Metaclassing in python:
What is a metaclass in Python?
http://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/
And I read about the ABC(abstract base classes) which is presented…

arthas_dk
- 443
- 5
- 15
12
votes
1 answer
Python - an object can be its own type?
I was playing around with metaclasses in CPython 3.2.2, and I noticed it is possible to end up with a class that is its own type:
Python 3.2.2 (default, Sep 5 2011, 21:17:14)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license"…

James
- 3,191
- 1
- 23
- 39
11
votes
1 answer
How to type the __new__ method in a Python metaclass so that mypy is happy
I am trying to type the __new__ method in a metaclass in Python so that it pleases mypy. The code would be something like this (taken from pep-3115 - "Metaclasses in Python 3000" and stripped down a bit):
from __future__ import annotations
from…

Enrique Pérez Arnaud
- 342
- 2
- 7
11
votes
2 answers
The call order of python3 metaclass
I am in confusion when trying to understand the order that metaclass creates a class instance. According to this diagram (source),
I type the following codes to verify it.
class Meta(type):
def __call__(self):
print("Meta __call__")
…

return long
- 183
- 2
- 9
11
votes
2 answers
Using ABC, PolymorphicModel, django-models gives metaclass conflict
So far every other answer on SO answers in the exact same way: construct your metaclasses and then inherit the 'joined' version of those metaclasses, i.e.
class M_A(type): pass
class M_B(type): pass
class A(metaclass=M_A): pass
class…

mastachimp
- 438
- 3
- 14