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
50
votes
7 answers

How to read class attributes in the same order as declared?

I am writing a metaclass that reads class attributes and store them in a list, but I want the list (cls.columns) to respect the declaration order (that is: mycol2, mycol3, zut, cool, menfin, a in my example): import inspect import pprint class…
Eric
  • 5,101
  • 10
  • 37
  • 45
45
votes
2 answers

Python metaclasses vs class decorators

What are the main differences between Python metaclasses and class decorators? Is there something I can do with one but not with the other?
gpilotino
  • 13,055
  • 9
  • 48
  • 61
42
votes
2 answers

Triple inheritance causes metaclass conflict... Sometimes

Looks like I stumbled upon a metaclass hell even when I didn't wanted anything to do with it. I'm writing an app in Qt4 using PySide. I want to separate event-driven part from UI definition, which is generated from Qt Designer files. Hence I create…
Red
  • 1,450
  • 2
  • 17
  • 33
40
votes
2 answers

How are Python metaclasses different from regular class inheritance?

This might be too much of an open ended question, but I'm just now learning about metaclasses in Python and I don't understand how a metaclass is any different than just having a child class inherit from the parent class, like class…
user2897013
  • 441
  • 6
  • 12
39
votes
2 answers

How to pass arguments to the metaclass from the class definition?

I'm trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object. I've read this post, which is awesome, but doesn't quite answer the question. at the moment I am…
yarbelk
  • 7,215
  • 6
  • 29
  • 37
37
votes
4 answers

Metaclass Mixin or Chaining?

Is it possible to chain metaclasses? I have class Model which uses __metaclass__=ModelBase to process its namespace dict. I'm going to inherit from it and "bind" another metaclass so it won't shade the original one. First approach is to subclass…
kolypto
  • 31,774
  • 17
  • 105
  • 99
37
votes
1 answer

Customary To Inherit Metaclasses From type?

I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass like def metacls(clsName, bases, atts): .... …
Nikwin
  • 6,576
  • 4
  • 35
  • 43
35
votes
2 answers

"MetaClass", "__new__", "cls" and "super" - what is the mechanism exactly?

I have read posts like these: What is a metaclass in Python? What are your (concrete) use-cases for metaclasses in Python? Python's Super is nifty, but you can't use it But somehow I got confused. Many confusions like: When and why would I have…
JV.
  • 2,658
  • 4
  • 24
  • 36
35
votes
2 answers

Automatically setting an enum member's value to its name

I've been messing around with python's enum library and have come across a conundrum. In the docs, they show an example of an auto-numbering enum, wherein something is defined: class Color(AutoNumber): red = () green = () ... I want to…
Keelan Armstrong
  • 351
  • 1
  • 3
  • 3
35
votes
1 answer

What does Python's builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don't know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal helper function used by the class statement. What does…
u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
33
votes
1 answer

__metaclass__ in Python 3

In Python2.7 this code can work very well, __getattr__ in MetaTable will run. But in Python 3 it doesn't work. class MetaTable(type): def __getattr__(cls, key): temp = key.split("__") name = temp[0] alias = None …
wyx
  • 3,334
  • 6
  • 24
  • 44
33
votes
9 answers

What are Python metaclasses useful for?

What can be done with metaclasses that can't be in any other way? Alex Martelli told that there are tasks that can't be achieved without metaclasses here Python metaclasses vs class decorators I'd like to know which are?
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
30
votes
3 answers

Dynamically define named classes in Ruby

I am writing an internal DSL in Ruby. For this, I need to programmatically create named classes and nested classes. What is the best way to do so? I recon that there are two ways to do so: Use Class.new to create an anonymous class, then use…
Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49
30
votes
3 answers

How to wrap every method of a class?

I'd like to wrap every method of a particular class in python, and I'd like to do so by editing the code of the class minimally. How should I go about this?
rjkaplan
  • 3,138
  • 5
  • 27
  • 33
29
votes
7 answers

Auto-register class methods using decorator

I want to be able to create a python decorator that automatically "registers" class methods in a global repository (with some properties). Example code: class my_class(object): @register(prop1,prop2) def my_method( arg1,arg2 ): #…
adamk
  • 45,184
  • 7
  • 50
  • 57
1
2
3
78 79