metaclasses Flashcards
1
Q
define class with metaclass
A
class Widget(object, metaclass=type): pass
2
Q
class definition in detail
A
namespace = metaclass.\_\_prepare\_\_(name, bases, **kwargs) Wiget = metaclass.\_\_new\_\_(metaclass, name, bases, namespace, **kwargs) metaclass.\_\_init\_\_(Widget, name, bases, namespace, **kwargs)
3
Q
when __prepare__ override ?
A
cusomize the type or initial value of the namespace mapping
4
Q
when __new__ override?
A
allocate and optionally configure new class object
5
Q
where **kwargs comes from?
A
from class definition class MyClass(metaclass=MyMetaclass, kwarg1 ="asdf):
6
Q
metamethod difference from @classmethod
A
metamethod is not visible from instance
7
Q
a = MyClass() What method invoked?
A
MyClass.__class__.__call__()