ast wrote: > Hi > > On python doc here: > > https://docs.python.org/3.4/reference/datamodel.html > > it is said about __prepare__ metaclass's method: > > If the metaclass has a __prepare__ attribute, it is called as > namespace = metaclass.__prepare__(name, bases, **kwds) > where the additional keyword arguments, if any, come from > the class definition. > > I don't understand what they call the "class definition". > > So I took their example and add a print(kwds) > > class OrderedClass(type): > > @classmethod > def __prepare__(metacls, name, bases, **kwds): > print(kwds) > return collections.OrderedDict() > > def __new__(cls, name, bases, namespace, **kwds): > result = type.__new__(cls, name, bases, dict(namespace)) > result.members = tuple(namespace) > return result > > class A(metaclass=OrderedClass): > def one(self): pass > def two(self): pass > def three(self): pass > def four(self): pass > > but print(kwds) outputs an empty dictionnary {} > > So what kwds is supposed to contains ? > > Thx
>>> class T(type): ... def __new__(*args, **kw): return type.__new__(*args) ... def __prepare__(*args, **kw): ... print(kw) ... return {} ... def __init__(*args, **kw): ... pass ... >>> class A(metaclass=T, answer=42): ... pass ... {'answer': 42} Adapted from <http://martyalchin.com/2011/jan/20/class-level-keyword-arguments/>. -- https://mail.python.org/mailman/listinfo/python-list