Linked Questions
354 questions linked to/from What are metaclasses in Python?
161 votes
2 answers
12k views
How to run code when a class is subclassed? [duplicate]
Is there a way to trigger code when my class is subclassed? class SuperClass: def triggered_routine(subclass): print("was subclassed by " + subclass.__name__) ...
96 votes
1 answer
31k views
Understanding metaclass and inheritance in Python [duplicate]
I have some confusion regarding meta-classes. With inheritance class AttributeInitType(object): def __init__(self,**kwargs): for name, value in kwargs.items(): setattr(self, ...
40 votes
2 answers
4k views
How are Python metaclasses different from regular class inheritance? [duplicate]
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 ...
29 votes
3 answers
10k views
What is the difference between a 'Type' and an 'Object' in Python [duplicate]
I came upon this reading the python documentation on the super keyword: If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, ...
15 votes
2 answers
3k views
How does one create a metaclass? [duplicate]
I have a rough idea of what meta-classes are. They are the classes of which class objects are based on (because classes are objects in Python). But could someone explain (with code) how one goes about ...
37 votes
1 answer
795 views
Can we overload behavior of class object [duplicate]
I know we can overload behavior of instances of a class, e.g. - class Sample(object): pass s = Sample() print s <__main__.Sample object at 0x026277D0> print Sample <class '__main__.Sample'&...
2 votes
1 answer
16k views
What does super().__init__(*args, **kwargs) do? [duplicate]
I don't know what does super().__init__(*args, **kwargs) do here. class B(type): def __init__(self, *args, **kwargs): self.a = 'a' super().__init__(*args, **kwargs) class A(...
12 votes
0 answers
8k views
What is the difference between Abstract Classes and Metaclasses in python? [duplicate]
I read two articles about Metaclassing in python: What is a metaclass in Python? http://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/ And I read about the ABC(abstract base ...
4 votes
1 answer
730 views
Use of isinstance() can overwrite type [duplicate]
Use of isinstance() changed the class type of dict Why is this happening? I know using builtins would prevent but I want to understand better why this is happening. 250 def printPretty(records,...
10 votes
0 answers
531 views
Why are django model classes not of type "type"? [duplicate]
Open a python shell on your django project root with python manage.py shell and observe the following: >>> class A(object): pass #checking type of user-defined class ... >>> type(A) ...
0 votes
1 answer
372 views
calling __call__ method on Singleton designed based on metaclass [duplicate]
I have the following Python code: class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(...
8 votes
0 answers
269 views
Python Metaclass __new__ method not getting called [duplicate]
Based on my limited Python knowledge Metaclass is another class. In the following example I am just building a metaclass. Here I overloaded the __new__ method. My expectation is at the end of the ...
6 votes
0 answers
170 views
Python Metaclasses are objects? [duplicate]
Instances are objects created from classes. Classes are objects too, but created from metaclasses. Functions are objects, methods are objects, files are objects, everything in Python is an object. ...
-3 votes
2 answers
79 views
Python: Converting python2 to python3 [duplicate]
I am trying to use https://github.com/iandees/mongosm/blob/master/insert_osm_data.py this package. It seems like it is written in Python2. I have converted all the way to next(context). However, I am ...
0 votes
1 answer
63 views
What are Arguments in Python Class Inheritance used for? [duplicate]
When browsing the source code for the django_filters library, I found a class declaration syntax that I've never seen before: inheritance with arguments. I could not find an explanation in the ...