2

Look at code in my python file:

class Point:
    def __init__(self):
        """ Create a new point at the origin """
        self.x = 0
        self.y = 0
p = Point()
p.x = 4
p.y = 4

I want to create another object q which contains all the values of p

Yes,we can do it like this: q = Point.objects.create(q.x = p.x,q.y = p.y)

But, I don't want to refer to all the variables inside the class Point

How can I do it ?

Light Yagami
  • 961
  • 1
  • 9
  • 29
  • You mean clone/copy? https://stackoverflow.com/questions/4794244/how-can-i-create-a-copy-of-an-object-in-python https://docs.python.org/2/library/copy.html – Sid Vishnoi Jun 21 '18 at 08:40
  • 3
    Possible duplicate of [How can I create a copy of an object in Python?](https://stackoverflow.com/questions/4794244/how-can-i-create-a-copy-of-an-object-in-python) – Reblochon Masque Jun 21 '18 at 08:42

1 Answers1

4
import copy

for a shallow copy use

p2 = copy.copy(p)

for a deep copy use

p2 = copy.deepcopy(p)
TomBombadil
  • 351
  • 1
  • 4
  • 15