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
10
votes
2 answers

Understanding __call__ with metaclasses

From my understanding the __call__ method inside a class implements the function call operator, for example: class Foo: def __init__(self): print("I'm inside the __init__ method") def __call__(self): print("I'm inside the…
electro7912
  • 339
  • 4
  • 14
10
votes
2 answers

Using __prepare__ for an Enum ... what's the catch?

Declarative usage of Python's enum.Enum requires values to be provided, when in the most basic use case for an enum we don't actually care about names and values. We only care about the sentinels themselves. After reading a related Q&A recently, I…
wim
  • 338,267
  • 99
  • 616
  • 750
10
votes
3 answers

Why does the class definition's metaclass keyword argument accept a callable?

Background The Python 3 documentation clearly describes how the metaclass of a class is determined: if no bases and no explicit metaclass are given, then type() is used if an explicit metaclass is given and it is not an instance of type(), then it…
Neil G
  • 32,138
  • 39
  • 156
  • 257
10
votes
3 answers

Implementing the factory design pattern using metaclasses

I found a lot of links on metaclasses, and most of them mention that they are useful for implementing factory methods. Can you show me an example of using metaclasses to implement the design pattern?
olamundo
  • 23,991
  • 34
  • 108
  • 149
10
votes
1 answer

Use a metaclass only for subclasses

Is there a way in Python 3.2 or later to define a class whose subclasses should be created using a specific metaclass without the class itself being created using that metaclass? An example to demonstrate what I mean: Let's say I want to create an…
Feuermurmel
  • 9,490
  • 10
  • 60
  • 90
10
votes
2 answers

iOS / Objective-C: Correct Way of Obtaining Meta Class Object

Which from the following is the correct way of obtaining the meta class? Class myMetaClass = objc_getMetaClass("NSString"); Or: Class myMetaClass = object_getClass([NSString class]); Are they both any different? As mentioned in another post that…
Unheilig
  • 16,196
  • 193
  • 68
  • 98
10
votes
1 answer

Python3 Singleton metaclass method not working

I saw a lot of methods of making a singleton in Python and I tried to use the metaclass implementation with Python 3.2 (Windows), but it doesn"t seem to return the same instance of my singleton class. class Singleton(type): _instances = {} …
Lord Gamez
  • 299
  • 7
  • 15
10
votes
1 answer

Using metaclasses to override methods of complex builtin

As a learning exercise, I'm trying to implement a class which will emulate the behavior of python's complex builtin, but with different behavior of the __str__ and __repr__ methods: I want them to print in the format... (1.0,2.0) ...instead…
mszep
  • 410
  • 3
  • 12
9
votes
1 answer

What is Groovy doing here?

I was trying to debug some code that uses mixins and I was able to reduce my problem down to this example. I have a parent class that receives methods via a mixin and a child class that inherits from the parent. If I try to replace a method on an…
mfollett
  • 1,814
  • 15
  • 27
9
votes
1 answer

Multiple inheritance metaclass conflict involving Enum

I need a double inheritance for a class that is an Enum but also support my own methods. Here's the context: import abc from enum import Enum class MyFirstClass(abc.ABC): @abc.abstractmethod def func(self): pass class…
Adam Boinet
  • 521
  • 8
  • 22
9
votes
1 answer

Python: Dynamically create class while providing arguments to __init_subclass__()

How can I dynamically create a subclass of my class and provide arguments to its __init_subclass__() method? Example class: class MyClass: def __init_subclass__(cls, my_name): print(f"Subclass created and my name is {my_name}") Normally…
Ellis Percival
  • 4,632
  • 2
  • 24
  • 34
9
votes
3 answers

Groovy adding code to a constructor

Is there a way in Groovy that I can add code to a constructor when a class is instantiated? I have a Groovy class (but I can't modify the source of this particular one), but I was hoping there was a way to inject code (maybe via the metaclass) so my…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
9
votes
2 answers

Arguments of __new__ and __init__ for metaclasses

I am a bit surprised by the method call order and the different arguments when overriding new and init in a metaclass. Consider the following: class AT(type): def __new__(mcs, name, bases, dct): print(f"Name as received in new: {name}") …
Cedric H.
  • 7,980
  • 10
  • 55
  • 82
9
votes
1 answer

How do I use generic typing with PyQt subclass without metaclass conflicts?

I had tried the abc.ABCMeta with sip wrapper type, and it works well when subclass with abc.ABC. class QABCMeta(wrappertype, ABCMeta): pass class WidgetBase(QWidget, metaclass=QABCMeta): ... class InterfaceWidget(WidgetBase, ABC): …
user7624126
  • 183
  • 1
  • 8
9
votes
3 answers

Decorating a class method after @property

I want to wrap every method of various objects except __init__ using a decorator. class MyObject(object): def method(self): print "method called on %s" % str(self) @property def result(self): return "Some derived…
Chiggs
  • 2,824
  • 21
  • 31