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
15
votes
1 answer
Avoiding sharing Java meta classes across different Groovy scripts
My situation
I call multiple Groovy scripts from Java, they both contain long-lived Groovy objects.
I would like my Groovy scripts to make some changes to a Java meta-class for a Java class (that have about 100 instances). However, the scripts…

Simon Forsberg
- 13,086
- 10
- 64
- 108
15
votes
1 answer
need to understand the flow of __init__, __new__ and __call__
class Singleton(type):
def __init__(self, *args, **kwargs):
print 'calling __init__ of Singleton class', self
print 'args: ', args
print 'kwargs: ', kwargs
super(Singleton, self).__init__(*args, **kwargs)
…

GodMan
- 2,561
- 2
- 24
- 40
14
votes
1 answer
How to define a Python metaclass with Boost.Python?
The Python C API has the PyObject *PyType_Type object, which is equivalent to type in the interpreter. If I want to define a metaclass in C++, how can I set type as one of its bases in Boost.Python? Also, what other things should I take into…

Paul Manta
- 30,618
- 31
- 128
- 208
14
votes
2 answers
Can I define a __repr__ for a class rather than an instance?
Can I define a __repr__ for a class rather than an instance? For example, I'm trying to do this
class A(object):
@classmethod
def __repr__(cls):
return 'My class %s' % cls
What I get is
In [58]: a=A()
In [59]: a
Out[59]: My class…

Wai Yip Tung
- 18,106
- 10
- 43
- 47
14
votes
1 answer
A django model that subclasses an abc, gives a metaclass conflict
I have a following model and abstract base class
import abc
from django.db import models
class AbstractBase():
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def my_method(self):
return
class MyModel(models.Model,…

yilmazhuseyin
- 6,442
- 4
- 34
- 38
14
votes
1 answer
TypeError: __init_subclass__() takes no keyword arguments
I'm trying to create a metaclass but when I assign it to another class I receive the error:
TypeError: __init_subclass__() takes no keyword arguments
But I don't implement any __init_subclass__. Why is this function being called?
class…

Robin De Schepper
- 4,942
- 4
- 35
- 56
14
votes
2 answers
Use Groovy Category implicitly in all instance methods of class
I have simple Groovy category class which adds method to String instances:
final class SampleCategory {
static String withBraces(String self) {
"($self)"
}
}
I want to use this category in my unit tests (for example). It looks…

oiavorskyi
- 2,893
- 1
- 20
- 23
14
votes
1 answer
How do I reference the GroovyObject instance from MetaClass methods in Groovy?
This is a contrived example of what I want to do, but minimally expresses the behavior desired. I want to reference the instance of the object on which the property access is being invoked. I tried 'this' first, but that refers to the enclosing…

Phil
- 909
- 1
- 10
- 17
14
votes
1 answer
nose, unittest.TestCase and metaclass: auto-generated test_* methods not discovered
This is a follow-up question for unittest and metaclass: automatic test_* method generation:
For this (fixed) unittest.TestCase layout:
#!/usr/bin/env python
import unittest
class TestMaker(type):
def __new__(cls, name, bases, attrs):
…

Santa
- 11,381
- 8
- 51
- 64
14
votes
1 answer
Add method to metaclass
I'm just playing with the metaclass programming in Groovy. But suddenly I was facing a little problem which I just could not get working...
Here is the simple script:
// define simple closure
def printValueClosure = {
println "The value is:…

Leikingo
- 890
- 3
- 10
- 23
14
votes
2 answers
Ruby metaclass madness
I'm stuck. I'm trying to dynamically define a class method and I can't wrap my head around the ruby metaclass model. Consider the following class:
class Example
def self.meta; (class << self; self; end); end
def self.class_instance; self;…

t6d
- 2,595
- 3
- 26
- 42
14
votes
3 answers
SqlAlchemy metaclass confusion
I'm trying to inject some of my own code in the class construction process of SqlAlchemy. Trying to understand the code, I'm somewhat confused by the implementation of the metaclass. Here are the relevant snippets:
The default "metaclass" of…

Achim
- 15,415
- 15
- 80
- 144
14
votes
3 answers
Adding base class to existing object in python
I have several objects of different kinds (different function names, different signatures) and I monkey patch them to have a common way to access them from different functions. Briefly, there is a dispatcher that takes the objects that I want to…

Hernan
- 5,811
- 10
- 51
- 86
14
votes
4 answers
Ruby metaclass confusion
I understand that all classes in ruby are instances of metaclass Class. And that "regular" objects are instances of these classes (the instances of metaclass Class).
But I keep wondering, I mean classes are root of objects, classes are themselves…

Perello
- 633
- 1
- 7
- 22
13
votes
3 answers
Python metaclass and ModGrammar
I found (after another question here on StackOverflow) this interesting library written in Python which goal is the grammar parsing.
http://code.google.com/p/modgrammar/
And I also found this tutorial regarding…

Dario
- 215
- 2
- 10