KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Today I have come across a surprising definition of a metaclass in Python here , with the metaclass definition effectively inlined. The relevant part is class Plugin(object): class __metaclass__(type): def __init__(cls, name, bases, dict): type.__init__(name, bases, dict) registry.append((name, cls)) When does it make sense to use such an inline definition? Further Arguments: An argument one way would be that the created metaclass is not reusable elsewhere using this technique. A counter argument is that a common pattern in using metaclasses is defining a metaclass and using it in one class and then inhertiting from that. For example, in a conservative metaclass the definition class DeclarativeMeta(type): def __new__(meta, class_name, bases, new_attrs): cls = type.__new__(meta, class_name, bases, new_attrs) cls.__classinit__.im_func(cls, new_attrs) return cls class Declarative(object): __metaclass__ = DeclarativeMeta def __classinit__(cls, new_attrs): pass could have been written as class Declarative(object): #code not tested! class __metaclass__(type): def __new__(meta, class_name, bases, new_attrs): cls = type.__new__(meta, class_name, bases, new_attrs) cls.__classinit__.im_func(cls, new_attrs) return cls def __classinit__(cls, new_attrs): pass Any other considerations?
Tags (comma-separated)
Save Edits
Cancel