Method Name:
copy
Method Signature:
copy()
Parameters:
none
Return Value:
A dictionary containing same object references as the source dictionary.
Method Overview:
Returns a shallow copy of the python dictionary object.
Example:
# Example Python program that does # Define a Point class with x,y attributes # String representation of the object # Create a dictionary # Populate the dictionary with instances of A print("Address of the dictionary:"); # Print the object references contained in the dictionary # Print the actual dictionary elements # Do a shallow copy of the dictionary print("Address of the new dictionary:"); print("Address of the elements in the new dictionary:"); # Print the elements from the new dictionary |
Output:
Address of the dictionary: 0x7fac39221d00 Address of dictionary elements: {0: <__main__.Point object at 0x7fac3922bfd0>, 1: <__main__.Point object at 0x7fac3922bf10>, 2: <__main__.Point object at 0x7fac3922be50>, 3: <__main__.Point object at 0x7fac3922bdf0>, 4: <__main__.Point object at 0x7fac3922bd90>} Contents of the dictionary 0: Point(0,0) 1: Point(1,2) 2: Point(2,4) 3: Point(3,6) 4: Point(4,8) Address of the new dictionary: 0x7fac39221440 Address of the elements in the new dictionary: {0: <__main__.Point object at 0x7fac3922bfd0>, 1: <__main__.Point object at 0x7fac3922bf10>, 2: <__main__.Point object at 0x7fac3922be50>, 3: <__main__.Point object at 0x7fac3922bdf0>, 4: <__main__.Point object at 0x7fac3922bd90>} Contents of the new dictionary 0: Point(0,0) 1: Point(1,2) 2: Point(2,4) 3: Point(3,6) 4: Point(4,8) |