返回顶部

收藏

Python通过深拷贝实现从实例继承

更多

Take an instance (call it foo) and create a factory class (call it InstanceFactory) 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

标签:python,深拷贝,OOD

收藏

0人收藏

支持

0

反对

0

发表评论