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
0
votes
1 answer

Python 2.7 inheritance from metaclasses

I've got a toy python example where defining an inline metaclass as a function behaves differently than defining it as a class, and I'm trying to understand why: >>> class Test(object): ... def __metaclass__(name, bases, nmspc): ... cls…
0
votes
0 answers

Deferred assignment of Python meta‑class parameters (kind of abstract meta‑class instance)

Say a meta‑class A expects parameters. A class B of this meta‑class, will have to assign these parameters. Say this class B is to be a base class, and that's its imediat derived classes which are expected to assign these parameters. Is this feasible…
Hibou57
  • 6,870
  • 6
  • 52
  • 56
0
votes
1 answer

Import modules in each other class in python using metaclass

I need to create a business query model, in which I need to create a circular dependency, I am using a look a like design of django models to implement it, #Modeule a.py import b class A: b_obj = B() a_property_1 =…
Aman Agarwal
  • 727
  • 6
  • 15
0
votes
3 answers

python scope issue with anonymous lambda in metaclass

I am using a metaclass to define read-only properties (accessor methods) for a class, by adding a property with only a getter (a lambda) for each field declared by the class. I am finding different behavior depending on where I define the lambda. …
Mike Slass
  • 35
  • 4
0
votes
0 answers

odd behavior of the "type" built-in python function when creating classes on the fly

I am working on a project in which I am using the API of some third party closed-source Django application in order to write a plugin that extends its functionality. The plugin consists of defining resource classes by inheriting from pre-made…
wissam
  • 1
  • 1
0
votes
1 answer

What is wrong with this metaclass implementation?

I am not saying that I need to, and I also saw this How do I create a simple metaclass? but what's wrong with this simple implementation: >>>class one(object): def __init__(self): class self:pass After this I…
sumit.viii
  • 11
  • 2
0
votes
1 answer

class_eval and context of class variables?

Looking for an explanation why in the following example in Module.class_eval block, class variables lookup does not work: class MyClass @@myvar = 123 def self.myvar_getter @@myvar end end class MyClass p "#{self}, #{self.object_id},…
David Unric
  • 7,421
  • 1
  • 37
  • 65
0
votes
2 answers

Saving argument method with MOP

I am doing some integration tests with Spock with 3rd party apps. Now I am struggling with a problem that I am not sure wether I am approaching the issue properly or not. In one of the tests I am connecting to a 3rd party service to get some…
Fran García
  • 2,011
  • 16
  • 24
0
votes
1 answer

ModelForm's metaclass prevents adding of extra fields

I'm writing a mixin class to dynamically add extra fields to a ModelForm which sublasses it. The new fields are generated in the mixin's __init__ method. See bellow: mixin class code (strongly shortened & simplified): class…
David Unric
  • 7,421
  • 1
  • 37
  • 65
0
votes
1 answer

How can I call the metaclass's __call__?

Given the following example on an instance of a X class: class X(): def __call__(self, incoming_list): print incoming_list X()([x for x in range(10)]) How can I obtain the same output by using the __call__ magic method from the class…
Ericson Willians
  • 7,606
  • 11
  • 63
  • 114
0
votes
1 answer

Ensure a Method is Overridden

I want to ensure that any class that is derived from my class overrides certain methods. If they are not overridden I want to raise a NotImplementedError as soon as possible after compiling begins, rather than when one of the methods are…
Griffin
  • 13,184
  • 4
  • 29
  • 43
0
votes
0 answers

Ruby class-level accessory behave differently from inside and outside of the class

Code is worth a thousand words: why this code acts as it does? class MyClass @variable = 0 class << self attr_accessor :variable end puts variable # => 0, as expected variable = 1 puts variable # => 1, as expected puts…
zmilojko
  • 2,125
  • 17
  • 27
0
votes
1 answer

How to deregister a class entry from Metaclass?

I am using Method 3: A metaclass from Creating a singleton in Python for creating object from singleton class. Now depending upon certain event, I want to delete this singleton object and remove its entry from dictionary maintained by metaclass. How…
Vikram
  • 1,999
  • 3
  • 23
  • 35
0
votes
4 answers

Changing the bases of an object based on arguments to __init__

I'm trying to create a metaclass in Python that dynamically changes the base class of a type, during creation depending upon the arguments given when creating the instance. In short I've got a hierarchy, C --> B --> A but what I want to do is…
Flexo
  • 87,323
  • 22
  • 191
  • 272
0
votes
2 answers

Python unittest and self registering classes

I just converted on of my classes to a "self registering" class. It was my first foray into python metaclasses. Oddly, many tests that had been passing started failing in odd ways. Many of those failing tests would pass when run by themselves and…
Timothy Grant
  • 141
  • 3
  • 10