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 metaclass 'nmspc' what does it mean?

I've seen 'nmspc' being used in the __init__ and __new__ methods of a metaclass in Python. What does 'nmspc' stand for? class SimpleMeta1(type): def __init__(cls, name, bases, nmspc): super(SimpleMeta1, cls).__init__(name, bases, nmspc) …
platypus
  • 1,165
  • 5
  • 27
  • 47
0
votes
2 answers

Why does a metaclass not have access to the attributes inhereited from a subclass of a class defined by the metaclass?

Class Foo is defined with a metaclass Meta. The metaclass loops over the class attributes and prints them to screen. Class Bar subclasses Foo. However, the metaclass does not print the inherited attributes from Bar. Why doesn't the metaclass have…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
0
votes
2 answers

Generate a list of class members that preserves their definition order

I am trying to automatically create some SQL tables from the definition of some Python classes, I tried using dir() but since it returns a Python Dictionary, it's not ordered so the definition order of the class members is lost. Reading on the…
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0
votes
1 answer

Why is this metaclass modifying the values only after the constructor executes?

class EmailAssist { def abcService = "abc" String name EmailAssist(String name) { this.name = name println abcService } EmailAssist() { } } EmailAssist.metaClass.getAbcService = {-> "test" } def e =…
mrfixij
  • 132
  • 1
  • 8
0
votes
0 answers

Metaclassing to solve dynamic class choosing with prevalidation?

I'm extending a Python ORM with an InType field, which is a validator. In fact, I am creating a whole number of validators... in web2py DAL terms I am emulating: Field(type=String, validators=[IS_EMAIL(), IS_IN(['foo@bar.com'])]) However doing it…
A T
  • 13,008
  • 21
  • 97
  • 158
0
votes
2 answers

Metaclass deligate not being instance

I have this stub of code to add dynamic attributes. I work with mongodb and i want to add the properties dynamically. This is what i tried to do when unit testing. User.metaClass.dynamicAttributes = [:] User.metaClass.propertyMissing = { String…
Amanuel Nega
  • 1,899
  • 1
  • 24
  • 42
0
votes
1 answer

How to get parent classes of class in my meta class?

I have the following script: #!/usr/bin/python3 class MyMeta(type): def __new__(mcs, name, bases, dct): print(name + " " + str(bases)) return super(MyMeta, mcs).__new__(mcs, name, bases, dct) class A(metaclass=MyMeta): def…
Denis
  • 3,595
  • 12
  • 52
  • 86
0
votes
2 answers

Android Groovy metaclass

I am trying writing some code on Groovy for Android and I stumbled upon a fact that I cannot use metaclass to add a property dynamically to an object: it.mapMarker.metaClass.project = it It fails with java.lang.NoClassDefFoundError: Failed…
cliffroot
  • 1,839
  • 17
  • 27
0
votes
1 answer

Metaclass mixing or chaining

Consider that code: class Meta(type): def __init__(cls, name, bases, attrs): def method(self): print('generated method call') cls.method = method super(Meta, cls).__init__(name, bases, attrs) class…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
0
votes
1 answer

Casting generic class inside generic function seems to change inference of type parameter

I have a weird situation where by changing T.self inside a generic function to T.self as T.Type, it changes the semantics of the code: class Foo { required init() {} } class Bar : Foo { } func f(_:T) -> T { return…
user102008
  • 30,736
  • 10
  • 83
  • 104
0
votes
1 answer

Python __new__ without calling another class's version of __new__?

What is an example of using __new__ in Python such that within the body of __new__ you do not return the result of calling some other (base, meta, or otherwise) class's version of __new__, but you do return an allocated instance of the intended…
ely
  • 74,674
  • 34
  • 147
  • 228
0
votes
1 answer

Groovy and the Metaclass

I am attempting to use the metaclass to overwrite a static method in a test. The method is: org.boon.HTTP.jsonRestCallWithHeaders It it is a Java class, and it is beyond my control. The declaration is: public static Response…
monksy
  • 14,156
  • 17
  • 75
  • 124
0
votes
1 answer

Python Metaclasses with Class Inheritance

So I've recently come to an understanding with inheritance for classes and have come to appreciate their uses. So I began coding a concrete example up for myself. I realized as I was making multiple classes for different weapons that it would be…
Kyrubas
  • 877
  • 8
  • 23
0
votes
2 answers

Can not assert type of an object?

Why this source... """ [...] """ # Import the standard date and time system. from datetime import datetime as dt # Ommited the remaining imports section class CuteClass(object): """ [...] """ def __init__(self, parameter_zero,…
1737973
  • 159
  • 18
  • 42
0
votes
2 answers

Django register ModelForm with Model

I have several MyModels, all derived from the same MyBaseModel, and for each model there is also a MyModelForm derived from MyModelBaseForm. Given a MyModelForm, I can access the related model through MyModelForm._meta.model. What would be the…
Quant Metropolis
  • 2,602
  • 2
  • 17
  • 16