python中的类可以由type
来动态的创建
type(name,bases,dct)
class
的名字metaclass
为python中的语法糖, 目的在于让python能够对于创建类进行操作和修改, 可以写出非常漂亮简洁的代码
class InterfaceMeta(type):
def __new__(cls, name, parents, dct):
# create a class_id if it's not specified
if 'class_id' not in dct:
dct['class_id'] = name.lower()
# open the specified file for writing
if 'file' in dct:
filename = dct['file']
dct['file'] = open(filename, 'w')
# we need to call type.__new__ to complete the initialization
return super(InterfaceMeta, cls).__new__(cls, name, parents, dct)
假设: 现在给定一个与数据库相关接口的基类, 它的子类实现了自己与数据库相关的接口, 当定义子类之后需要自动的注册在基类的注册中心中.
元类
class DBInterfaceMeta(type):
# we use __init__ rather than __new__ here because we want
# to modify attributes of the class *after* they have been
# created
def __init__(cls, name, bases, dct):
if not hasattr(cls, 'registry'):
# this is the base class. Create an empty registry
cls.registry = {}
else:
# this is a derived class. Add cls to the registry
interface_id = name.lower()
cls.registry[interface_id] = cls
super(DBInterfaceMeta, cls).__init__(name, bases, dct)
基类
class DBInterface(object):
__metaclass__ = DBInterfaceMeta
print(DBInterface.registry)
子类
class FirstInterface(DBInterface):
pass
class SecondInterface(DBInterface):
pass
class SecondInterfaceModified(SecondInterface):
pass
print(DBInterface.registry)
primer on python metaclass