On 2018-09-16 08:48, Ajay Patel wrote:
Hello,

Thanks for your reply.

What happen if do copy using copy module? i can see address are different when using copy module.
Which magic method will call when i an doing deepcopy?
Snippet>>>>>>

>>> p =Point(4,5)
__setattr__
__setattr__
__setattr__
>>> p
(4,5)
>>> id(p)
3072575564
>>> import copy
>>> p2 = copy.deepcopy(p)
>>> p
(4,5)
>>> id(p2)
3072522924
>>>

You don't necessarily have to do anything else; the copy/deepcopy operation will create a new instance and copy/deepcopy the appropriate attributes for you.

However, if you want more control, you can define a couple of dunder methods to do it your own way.

I'll quote from the documentation page on the 'copy' module:

|"""|In order for a class to define its own copy implementation, it can define special methods |__copy__()| and |__deepcopy__()|. The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the |__deepcopy__()| implementation needs to make a deep copy of a component, it should call the |deepcopy()| <#copy.deepcopy> function with the component as first argument and the memo dictionary as second argument."""


On Sun, Sep 16, 2018 at 1:12 AM MRAB <pyt...@mrabarnett.plus.com <mailto:pyt...@mrabarnett.plus.com>> wrote:

    On 2018-09-15 19:47, Ajay Patel wrote:
    >
    > I have created below code and i want to restrict an object copy.
    > What are the methods called for copy constructor and assignment
    operator? Basically i don't want to allow below operation.
    >
    > p = Point(1,3)
    > p2 = Point(6,7)
    >
    > => How to disallow below operations?
    > p(p2)
    > p = p2
    >
    > Please point out a documentation for the same if available.
    >
    >
    > class Point:
    >
    >          def _init_(self, x = 0, y = 0):
    >                  self.x = x
    >                  self.y = y
    >
    >          def _str_(self):
    >                  return "({0},{1})".format(self.x,self.y)
    >
    >          def _repr_(self):
    >                  return "({0},{1})".format(self.x,self.y)
    >
    >          def _call_(self,other):
    >                  print("_call_")
    >                  self.x = other.x
    >                  self.y = other.y
    >
    >          def _setattr_(self, name, value):
    >                  print("_setattr_",name,value)
    >

    "__init__", etc, are referred to as "dunder" methods because they
    have
    double leading and trailing underscores. Those that you wrote have
    only
    single leading and trailing underscores.

    The term "copy constructor" is something from C++. It doesn't
    exist in
    Python.

    Assignment statements _never_ copy an object. If you want a copy
    of an
    object, you have to be explicit.

    Writing:

    p = p2

    will merely make 'p' refer to the same object that 'p2' currently
    refers to.

    For making a copy of an object, have a look at the "copy" module.
-- https://mail.python.org/mailman/listinfo/python-list



--
*Er. Ajay A Patel*


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to