I have recently begun working with dill. I have a metaclass, which I use to create a Singleton pattern. Always have one object at any instant. I am using dill to serialise.The problem is once the object is loaded back, it doesn’t respect the Singleton pattern (enforced by metaclass) and __init__ gets ca…
Tag: metaclass
Python – What does it mean for a Class to be callable?
I am trying to understand what ‘callables’ are in Python and what it means for a class to be callable. I was playing with the following code: This gives the following result: Furthermore, Gives: Does this mean that class A has a __call__ method? and why is it of type class? is A.__call__() being c…
Dynamically creating Django models with `type`
I have 20+ MySQL tables, prm_a, prm_b, … with the same basic structure but different names, and I’d like to associate them with Django model classes without writing each one by hand. So, feeling ambitious, I thought I’d try my hand at using type() as a class-factory: The following works: But…
How to pass arguments to the metaclass from the class definition?
I’m trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object. I’ve read this post, which is awesome, but doesn’t quite answer the question. at the moment I am doing: but this requires me to do the following…
Using the __call__ method of a metaclass instead of __new__?
When discussing metaclasses, the docs state: You can of course also override other class methods (or add new methods); for example defining a custom __call__() method in the metaclass allows custom behavior when the class is called, e.g. not always creating a new instance. [Editor’s note: This was remov…