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
11
votes
3 answers
How to convert a string with the name of a class to the class type itself?
In order to store a class name in a log file I converted the description of a class type to a string:
let objectType: NSObject.Type = Object.self
let str = String(describing: objectType)
However, I do not succeed in back conversion of str to a…

Taco
- 701
- 7
- 21
11
votes
3 answers
Generic metaclass to keep track of subclasses?
I'm trying to write a generic metaclass to track subclasses
Since I want this to be generic, I didn't want to hardcode any class name within this metaclass, therefore I came up with a function that generates the proper metaclass, something like:
def…

Carles Barrobés
- 11,608
- 5
- 46
- 60
11
votes
1 answer
Destructor in metaclass Singleton object
I'm modifying a legacy library that uses the singleton pattern through the metaclass approach.
The Singleton class, inheriting from type, defines de __call__ function.
Right now, my singleton object using this library are never deleted. I defined…

JoseLSegura
- 3,830
- 3
- 20
- 27
11
votes
3 answers
Who calls the metaclass
This actually stems from a discussion here on SO.
Short version
def meta(name, bases, class_dict)
return type(name, bases, class_dict)
class Klass(object):
__metaclass__ = meta
meta() is called when Klass class declaration is…

dhke
- 15,008
- 2
- 39
- 56
11
votes
2 answers
What is Ruby's analog to Python Metaclasses?
Python has the idea of metaclasses that, if I understand correctly, allow you to modify an object of a class at the moment of construction. You are not modifying the class, but instead the object that is to be created then initialized.
Python (at…

Sean Copenhaver
- 10,435
- 1
- 18
- 16
11
votes
2 answers
Python: Class factory using user input as class names
I want to add class atttributes to a superclass dynamically. Furthermore, I want to create classes that inherit from this superclass dynamically, and the name of those subclasses should depend on user input.
There is a superclass "Unit", to which I…

Sano98
- 419
- 2
- 7
- 17
11
votes
2 answers
What does class_getClassVariable() do?
If instance variables belong to an instance of a class, class variables would belong to an instance of a metaclass, I should think. But my experience with the Objective-C metaclass tells me that this is unlikely.
I'm wondering what…

Jonathan Sterling
- 18,320
- 12
- 67
- 79
11
votes
2 answers
What is the purpose of `__metaclass__ = type`?
Python (2 only?) looks at the value of variable __metaclass__ to determine how to create a type object from a class definition. It is possible to define __metaclass__ at the module or package level, in which case it applies to all subsequent class…

Mechanical snail
- 29,755
- 14
- 88
- 113
11
votes
2 answers
Sphinx document module properties
I have a module that should have a @property, I solved this by setting a class as the module. I got the idea from this answer: Lazy module variables--can it be done?
I wanted this to be repeatable and easy to use so I made a metaclass for it. This…

siebz0r
- 18,867
- 14
- 64
- 107
11
votes
2 answers
Dynamically adding methods with or without metaclass
Update - 2012/12/13
Just to clarify - I'm not so much interested in ways how to add methods to classes - as you can see below in my question and in people's answers, there is more than one way to do that (tongue in cheek and hat tip to my Perl…

TomasHeran
- 129
- 1
- 6
11
votes
1 answer
python dynamically create class with inner class
I'm using django-tastypie and I need to create classes like this from my django models:
class MyModelResource(ModelResource):
class Meta:
queryset = MyModel.objects.all()
allowed_methods = ['get']
Since I have a lot of models in…

mnowotka
- 16,430
- 18
- 88
- 134
11
votes
1 answer
TypeError: object.__new__(int) is not safe, use int.__new__()
While reading this: What is a metaclass in Python?, I am learning to use __new__ using the following snippet:-
class a(object):
pass
a.__new__(int,'abcdef',(int,),{})
There could be some problem in calling __new__ using a.. But, I get the…

GodMan
- 2,561
- 2
- 24
- 40
10
votes
2 answers
How to add method using metaclass
How do I add an instance method to a class using a metaclass (yes I do need to use a metaclass)? The following kind of works, but the func_name will still be "foo":
def bar(self):
print "bar"
class MetaFoo(type):
def __new__(cls, name,…

Knut Eldhuset
- 1,789
- 3
- 16
- 19
10
votes
1 answer
Python abc inheritance with specified metaclass
What is the proper way of inheriting from ABC if class have metaclass specified?
Straightforward attempt
class KindMeta(type):
...
class Kind(ABC, metaclass=KindMeta):
...
resulted in TypeError: metaclass conflict: the metaclass of a…

Sadderdaze
- 304
- 3
- 14
10
votes
1 answer
__new__, __init__, and metaclasses (and superclasses)
I've been spending the day trying to understand the intricacies of the python class model, messing around with decorators, metaclasses, and superclasses.
Currently, I'm trying to figure out the role of certain token functions, namely new (back-story…

Anthony
- 1,015
- 8
- 22