Questions tagged [metaclass]

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.

1179 questions
7262
votes
25 answers

What are metaclasses in Python?

What are metaclasses? What are they used for?
Bite code
  • 578,959
  • 113
  • 301
  • 329
1621
votes
37 answers

Creating a singleton in Python

This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented in Python in such a way that is most pythonic. In…
theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
1002
votes
6 answers

class << self idiom in Ruby

What does class << self do in Ruby?
randombits
  • 47,058
  • 76
  • 251
  • 433
170
votes
21 answers

What are some (concrete) use-cases for metaclasses?

I have a friend who likes to use metaclasses, and regularly offers them as a solution. I am of the mind that you almost never need to use metaclasses. Why? because I figure if you are doing something like that to a class, you should probably be…
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
120
votes
5 answers

Understanding __init_subclass__

I finally upgraded my python version and I was discovering the new features added. Among other things, I was scratching my head around the new __init_subclass__ method. From the docs: This method is called whenever the containing class is…
EsotericVoid
  • 2,306
  • 2
  • 16
  • 25
91
votes
1 answer

Understanding metaclass and inheritance in Python

I have some confusion regarding meta-classes. With inheritance class AttributeInitType(object): def __init__(self,**kwargs): for name, value in kwargs.items(): setattr(self, name, value) class Car(AttributeInitType): def…
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
87
votes
6 answers

Resolving metaclass conflicts

I need to create a class that uses a different base class depending on some condition. With some classes I get the infamous: TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all…
Yves Dorfsman
  • 2,684
  • 3
  • 20
  • 28
73
votes
6 answers

Using the __call__ method of a metaclass instead of __new__?

When discussing metaclasses, the docs state: You can of course also override other class methods (or add new methods); for example defining a custom __call__() method in the metaclass allows custom behavior when the class is called, e.g. not always…
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
65
votes
2 answers

python subclasscheck & subclasshook

The methods __subclasscheck__ and __subclasshook__ are used to determine if a class is regarded as subclass of another. However, their documentation is very limited, even in advanced Python books. How are they meant to be used and what is their…
blue_note
  • 27,712
  • 9
  • 72
  • 90
62
votes
2 answers

Python Metaclass : Understanding the 'with_metaclass()'

I want to ask what the with_metaclass() call means in the definition of a class. E.g.: class Foo(with_metaclass(Cls1, Cls2)): Is it a special case where a class inherits from a metaclass? Is the new class a metaclass, too?
Zakos
  • 2,448
  • 4
  • 16
  • 20
61
votes
5 answers

Is there any reason to choose __new__ over __init__ when defining a metaclass?

I've always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
60
votes
4 answers

Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.5

I'd like to create a class which has abc.ABCMeta as a metaclass and is compatible both with Python 2.7 and Python 3.5. Until now, I only succeeded doing this either on 2.7 or on 3.5 - but never on both versions simultaneously. Could someone give me…
Tatiana Al-Chueyr
  • 1,065
  • 1
  • 9
  • 11
59
votes
6 answers

How to auto register a class when it's defined

I want to have an instance of class registered when the class is defined. Ideally the code below would do the trick. registry = {} def register( cls ): registry[cls.__name__] = cls() #problem here return cls @register class MyClass( Base…
deft_code
  • 57,255
  • 29
  • 141
  • 224
59
votes
2 answers

Metaclass multiple inheritance inconsistency

Why is this: class MyType(type): def __init__(cls, name, bases, attrs): print 'created', cls class MyMixin: __metaclass__ = MyType class MyList(list, MyMixin): pass okay, and works as expected: created
Matt Anderson
  • 19,311
  • 11
  • 41
  • 57
58
votes
4 answers

Error when calling the metaclass bases: function() argument 1 must be code, not str

I tried to subclass threading.Condition earlier today but it didn't work out. Here is the output of the Python interpreter when I try to subclass the threading.Condition class: >>> import threading >>> class ThisWontWork(threading.Condition): ... …
David Underhill
  • 15,896
  • 7
  • 53
  • 61
1
2 3
78 79