Python通过深拷贝实现从实例继承,python实例,Take an inst


Take an instance (call it foo) and create a factory class (call itInstanceFactory) that produces foo's. Then inherit from InstanceFactory.

def MakeClassFromInstance(instance):    from copy import deepcopy    copy = deepcopy(instance.__dict__)    InstanceFactory = type('InstanceFactory', (instance.__class__, ), {})    InstanceFactory.__init__ = lambda self, *args, **kwargs: self.__dict__.update(copy)    return InstanceFactory

评论关闭