metaclass

Subclassed django models with integrated querysets

孤街浪徒 提交于 2019-12-21 04:37:09
问题 Like in this question, except I want to be able to have querysets that return a mixed body of objects: >>> Product.objects.all() [<SimpleProduct: ...>, <OtherProduct: ...>, <BlueProduct: ...>, ...] I figured out that I can't just set Product.Meta.abstract to true or otherwise just OR together querysets of differing objects. Fine, but these are all subclasses of a common class, so if I leave their superclass as non-abstract I should be happy, so long as I can get its manager to return objects

Who calls the metaclass

孤街醉人 提交于 2019-12-21 04:03:18
问题 This actually stems from a discussion here on SO. Short version def meta(name, bases, class_dict) return type(name, bases, class_dict) class Klass(object): __metaclass__ = meta meta() is called when Klass class declaration is executed. Which part of the (python internal) code actually calls meta() ? Long version When the class is declared, some code has to do the appropriate attribute checks and see if there is a __metaclass__ declared on a type. If such exists, it has to perform a method

Why does the class definition's metaclass keyword argument accept a callable?

霸气de小男生 提交于 2019-12-20 21:01:41
问题 Background The Python 3 documentation clearly describes how the metaclass of a class is determined: if no bases and no explicit metaclass are given, then type() is used if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass if an instance of type() is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used Therefore, according to the second rule, it is possible to specify a metaclass using a

Why should I use the __prepare__ method to get a class' namespace?

北战南征 提交于 2019-12-20 19:09:24
问题 Note This question is not about the Python 3 Enum data type, it's just the example I'm using. With PEP 3115 Python 3 added the __prepare__ 1 method to type for the purpose of allowing a custom namespace to be used when creating classes. For example, the new Enum data type uses __prepare__ to return an instance of the private _EnumDict for use as the new Enum class' namespace. However, I have seen several examples on SO 2 of EnumMeta being subclassed, creating a new namespace for the class in

Why should I use the __prepare__ method to get a class' namespace?

北城余情 提交于 2019-12-20 19:09:07
问题 Note This question is not about the Python 3 Enum data type, it's just the example I'm using. With PEP 3115 Python 3 added the __prepare__ 1 method to type for the purpose of allowing a custom namespace to be used when creating classes. For example, the new Enum data type uses __prepare__ to return an instance of the private _EnumDict for use as the new Enum class' namespace. However, I have seen several examples on SO 2 of EnumMeta being subclassed, creating a new namespace for the class in

How to pass arguments to the metaclass from the class definition in Python 3.x?

五迷三道 提交于 2019-12-20 10:34:53
问题 This is a Python 3.x version of the How to pass arguments to the metaclass from the class definition? question, listed separately by request since the answer is significantly different from Python 2.x. In Python 3.x, how do I pass arguments to a metaclass's __prepare__ , __new__ , and __init__ functions so a class author can give input to the metaclass on how the class should be created? As my use case, I'm using metaclasses to enable automatic registration of classes and their subclasses

What is the difference between type and type.__new__ in python?

冷暖自知 提交于 2019-12-20 09:18:04
问题 I was writing a metaclass and accidentally did it like this: class MetaCls(type): def __new__(cls, name, bases, dict): return type(name, bases, dict) ...instead of like this: class MetaCls(type): def __new__(cls, name, bases, dict): return type.__new__(cls, name, bases, dict) What exactly is the difference between these two metaclasses? And more specifically, what caused the first one to not work properly (some classes weren't called into by the metaclass)? 回答1: In the first example you're

Understanding __init_subclass__

江枫思渺然 提交于 2019-12-20 09:09:29
问题 I finally upgraded my python version and I was discovering the new features added. Among other things, I was scratching my head around the new __init_subclass__ method. From the docs: This method is called whenever the containing class is subclassed. cls is then the new subclass. If defined as a normal instance method, this method is implicitly converted to a class method. So I started to playing around with it a little bit, following the example in the docs: class Philosopher: def __init

Customary To Inherit Metaclasses From type?

与世无争的帅哥 提交于 2019-12-20 08:37:08
问题 I have been trying to understand python metaclasses, and so have been going through some sample code. As far as I understand it, a Python metaclass can be any callable. So, I can have my metaclass like def metacls(clsName, bases, atts): .... return type(clsName, bases, atts) However, I have seen a lot of people write their metaclasses in the following way: class Metacls(type): def __new__(meta, clsName, bases, atts): .... return type.__new__(meta, clsName, bases, atts) As far as I can see,

What are Python metaclasses useful for?

旧时模样 提交于 2019-12-20 08:35:17
问题 What can be done with metaclasses that can't be in any other way? Alex Martelli told that there are tasks that can't be achieved without metaclasses here Python metaclasses vs class decorators I'd like to know which are? 回答1: Metaclasses are indispensable if you want to have class objects (as opposed to instances of class objects) equipped with "special customized behavior", since an object's behavior depends on special methods on the type of the object, and a class object's type is, exactly