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
0
votes
1 answer
Cannot call base __init__ in a class created with type()
I create a class with type as I need to use a variable for the base.
It works fine if I don't provide an __init__ method, but when I do provide one, I am unable to call the base's __init__.
Works:
def constructor():
blah blah
api =…

Yves Dorfsman
- 2,684
- 3
- 20
- 28
0
votes
0 answers
Groovy override or intercept Java method
Using Groovy 2.0
Just as background I am rolling Yaml with SnakeYaml into Groovy classes. SnakeYaml uses Java reflection to construct and invoke the setting of properties. I want to with certain classes (that are created from Yaml) to allow for…

Matthew Campbell
- 1,864
- 3
- 24
- 51
0
votes
1 answer
Grails behavior difference between run-app and run-war
I'm relatively new to Groovy and Grails and am trying them out in my spare time. I've got a small test Grails application that I'm able to run fine using grails run-app, but grails run-war results in an error.
In the grails-app/conf/BootStrip.init…

Alan Krueger
- 4,701
- 4
- 35
- 48
-1
votes
1 answer
How to recreate the same "base and instance of" relationship that `object` and `type` hold in Python?
I'm learning knowledge about metaclass recently. I learnt that isinstance(object, type) and issubclass(type, object). I want to write self-defined class act like object and type, but how to declare class when circular dependency is happened? the…

wangjianyu
- 97
- 4
-1
votes
1 answer
How to get default arguments from concrete __init__ into a metaclass' __call__?
Please consider
class Meta(type):
def __call__(cls, *args, **kwargs):
print(cls)
print(f"args = {args}")
print(f"kwargs = {kwargs}")
super().__call__(*args, **kwargs)
class Actual(metaclass=Meta):
def…

Gulzar
- 23,452
- 27
- 113
- 201
-1
votes
1 answer
Dynamically setting properties with metaclass
I'm facing a bug when creating a metaclass that dynamically creates classes with properties given a config file.
In more detail, I expect to dynamically create classes with some properties (regular setter and a getter that runs the result of a…

Eduardo
- 631
- 1
- 12
- 25
-1
votes
1 answer
Python add methods of objects to containing list
I have a list containing objects of same class. now I want to make that list a container class that relates to all methods of the objects it contains as you can see in my example below. But since I have a lot of methods, I don't want to explicitly…

erg erg
- 3
- 2
-1
votes
1 answer
How to check an argument type before __init__ gets called
I need to check the argument type in __init__(). I did it this way:
class Matrix:
def __init__(self, matrix):
"""
if type(matrix) != list raise argument error
"""
if type(matrix) != list:
raise…

kaktus_car
- 986
- 2
- 11
- 19
-1
votes
2 answers
How to access variables defined in subclasses of a class from that class?
I have defined a base class somewhere, that is extendable by some user.
Now i want to access variables potentially defined by that user in his subclasses so that i can do something with it.
My attempt to solve this problem is by using…

moctarjallo
- 1,479
- 1
- 16
- 33
-1
votes
1 answer
Why python super() not working without parameters in ABCMETA class?
I have a problem with understanding function super() behavior in abc.ABCMeta class in python3.6.
class ParentClass():
def test():
return 1
@six.add_metaclass(ABCMeta)
class ChildClass(ParentClass):
def test(self):
test_ =…

Dmitry Kurgansky
- 23
- 5
-1
votes
1 answer
Python metaclasses tutorial
Sorry if I have to use you as Google, but Google does not help. I was looking for a very good tutorial about metaclasses in Python. One feature I remember about it was a three-paned image where you had metaclasses, classes and instances, one for…

Stefano Borini
- 138,652
- 96
- 297
- 431
-1
votes
2 answers
why global variable __metaclass__ not working?
I define some func here, it will change all user defined attribtutes into upper case
def up(name, parent, attr):
user_defined_attr = ((k, v) for k, v in attr.items() if not k.startswith('_'))
up_attr = {k.upper(): v for k,v in…

Kramer Li
- 2,284
- 5
- 27
- 55
-1
votes
1 answer
Python 3 type is both an object and class?
I am reading Learning python. Regarding metaclass, the book said type is an object and also is itself class. I am trying to search python doc online but don't see any similar description. Can someone point me any official doc?
Thinking a bit more on…

SSY
- 627
- 4
- 10
-1
votes
1 answer
Python best practice for creating a class that wraps many different types of classes with the same base
What is best practice in Python for creating a class that wraps many different types of classes with the same base class?
In my specific situation, I have a base class (1st layer) that represents a generalized data feed, and multiple classes (2nd…

Kamil
- 11
- 3
-1
votes
1 answer
What's relationship between dict and attrs in python?
I'm a beginner of python. I was learning meta-class but I did not quite understand how dictionary store methods and attributes?
class ModelMetaclass(type):
def __new__(cls, name, bases, attrs):
if name=='Model':
return…

Chen Loki
- 3
- 2