Copy constructor in python?

后端 未结 7 909
逝去的感伤
逝去的感伤 2020-11-29 02:29

Is there a copy constructor in python ? If not what would I do to achieve something similar ?

The situation is that I am using a library and I have extended one of t

7条回答
  •  抹茶落季
    2020-11-29 02:39

    A simple example of my usual implementation of a copy constructor:

    import copy
    
    class Foo:
    
      def __init__(self, data):
        self._data = data
    
      @classmethod
      def from_foo(cls, class_instance):
        data = copy.deepcopy(class_instance._data) # if deepcopy is necessary
        return cls(data)
    

提交回复
热议问题