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
21
votes
2 answers
How to inherit from an abstract base class written in C#
I’m trying to inherit from an abstract .NET base class in Python (2.7) using Python.NET (2.1.0). I’m a Python n00b but from what I understood…
Here’s what I managed to do in Python only and which works fine:
import abc
class Door(object):
…

freefall
- 388
- 1
- 14
20
votes
4 answers
Ways to make a class immutable in Python
I'm doing some distributed computing in which several machines communicate under the assumption that they all have identical versions of various classes. Thus, it seems to be good design to make these classes immutable; not in the sense that it must…

porgarmingduod
- 7,668
- 10
- 50
- 83
19
votes
1 answer
Body of abstract method in Python 3.5
I have an abstract class, Model, with a few abstract methods, what should I put in the body of the methods?
A return
class Model(metaclass=ABCMeta):
@abstractmethod
def foo(self):
return
A pass
class Model(metaclass=ABCMeta):
…

shayaan
- 1,482
- 1
- 15
- 32
19
votes
3 answers
Python ABC Multiple Inheritance
I think the code will explain the problem better than I can do with words. Here is the code in my_abc.py:
from abc import ABCMeta, abstractmethod
class MyABC(object):
__metaclass__ = ABCMeta
@abstractmethod
def print(self):
…

moeabdol
- 4,779
- 6
- 44
- 43
18
votes
4 answers
What are the differences between a `classmethod` and a metaclass method?
In Python, I can create a class method using the @classmethod decorator:
>>> class C:
... @classmethod
... def f(cls):
... print(f'f called with cls={cls}')
...
>>> C.f()
f called with cls=
Alternatively, I…

user200783
- 13,722
- 12
- 69
- 135
18
votes
2 answers
metaclass and __prepare__ ()
I am teaching myself about the __prepare__ function. And I see this snippet at PEP3115
# The custom dictionary
class member_table(dict):
def __init__(self):
self.member_names = []
def __setitem__(self, key, value):
# if the…

luoshao23
- 391
- 2
- 14
18
votes
4 answers
How to pass arguments to the metaclass from the class definition in Python 3.x?
This is a Python 3.x version of the How to pass arguments to the metaclass from the class definition? question, listed separately by request since the answer is significantly different from Python 2.x.
In Python 3.x, how do I pass arguments to a…

John Crawford
- 2,144
- 2
- 19
- 16
18
votes
4 answers
Python metaclasses: Why isn't __setattr__ called for attributes set during class definition?
I have the following python code:
class FooMeta(type):
def __setattr__(self, name, value):
print name, value
return super(FooMeta, self).__setattr__(name, value)
class Foo(object):
__metaclass__ = FooMeta
FOO = 123
…

ThiefMaster
- 310,957
- 84
- 592
- 636
17
votes
5 answers
Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?
I recently discovered metaclasses in python.
Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on…

Mike A
- 1,273
- 12
- 13
17
votes
5 answers
Delphi class references... aka metaclasses... when to use them
I've read the official documentation and I understand what class references are but I fail to see when and why they are best solution compared to alternatives.
The example cited in the documentation is TCollection which can be instantiated with any…

Kenneth Cochran
- 11,954
- 3
- 52
- 117
16
votes
2 answers
Overriding __contains__ method for a class
I need to simulate enums in Python, and did it by writing classes like:
class Spam(Enum):
k = 3
EGGS = 0
HAM = 1
BAKEDBEANS = 2
Now I'd like to test if some constant is a valid choice for a particular Enum-derived class, with the…

clstaudt
- 21,436
- 45
- 156
- 239
15
votes
2 answers
Java implementation - Meta classes
The way I understand it, Java object model is 3 levels, each level describes the level beneath it, therefore there is one Meta class shared by all Classes (which are themselves objects?).
My question is - how are constructors implemented in Java?…

Shmoopy
- 5,334
- 4
- 36
- 72
15
votes
1 answer
Class that returns False with bool(TheClassItself)
I want to create a class MyClass where bool(MyClass) returns False. Is it possible?
I want this behavior with the class itself, not objects of that class. For objects of that class I know that I can just return False in __bool__(self).

Eduardo
- 185
- 6
15
votes
2 answers
How does one create a metaclass?
I have a rough idea of what meta-classes are. They are the classes of which class objects are based on (because classes are objects in Python). But could someone explain (with code) how one goes about creating one.

Tristan Pops Kildaire
- 303
- 1
- 8
15
votes
1 answer
When to inline definitions of metaclass in Python?
Today I have come across a surprising definition of a metaclass in Python here, with the metaclass definition effectively inlined. The relevant part is
class Plugin(object):
class __metaclass__(type):
def __init__(cls, name, bases,…

Muhammad Alkarouri
- 23,884
- 19
- 66
- 101