Python Forum
frozenset.copy() docs wrong
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
frozenset.copy() docs wrong
#1
i read in doc that frozenset.copy() returns a shallow copy but i got a deep copy. which is wrong? docs or implementation? i believe the docs are wrong.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Quote:i read in doc that frozenset.copy() returns a shallow copy
That is correct.

Quote:but i got a deep copy
What makes you say that? Can you show code in which this happens?
Reply
#3
Output:
t2a/forums /home/forums 3> py3 Python 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = frozenset([frozenset([frozenset(['foo','bar'])])]) >>> x frozenset({frozenset({frozenset({'foo', 'bar'})})}) >>> frozenset.copy(x) frozenset({frozenset({frozenset({'foo', 'bar'})})}) >>> lt2a/forums /home/forums 4>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
That just shows you have a copy.
Maybe you're not familiar with the terminology?
There's a definition of shallow vs deep copy at https://docs.python.org/3/library/copy.html
Reply
#5
can you be more specific about the Python terminology you think i'm not familiar with, as opposed to the generic terminology for shallow and deep copy?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
From the linked docs:
Quote:A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

So, when printed, both a shallow and deep copy will look the same:
>>> import copy
>>> original = [1, [2, 3], 4]
>>> shallow = original.copy()
>>> deep = copy.deepcopy(original)
>>> original
[1, [2, 3], 4]
>>> shallow
[1, [2, 3], 4]
>>> deep
[1, [2, 3], 4]
The difference is whether the objects inside the copy are references to the originals or copies:
>>> original[1] is shallow[1]
True
>>> original[1] is deep[1]
False
Reply
#7
so how do we test if the copies are references or reconstructions? id() ??
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
If you want to check if an object is a copy or a reference to the same object, using the is operator is probably simplest.
It only matters for mutable objects that you plan on modifying, though.
Reply
#9
i wasn't wanting to check for an application purpose, but to study that copy method. as far as i can see, for immutable objects like an instance of frozenset, there is no value to know if it is a copy or a reference to a shared instance. ultimately, whether a copy method of an immutable object does a (recursive) full copy or makes a new reference (however the implementation carries that out), it doesn't really matter, at all. so the difference is moot. there is no real point to define the difference besides beginners potentially not understanding why it might be omitted.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  where is documentation for stat result attributes in PDF docs? Skaperen 5 998 May-18-2023, 07:24 PM
Last Post: Skaperen
  when would a frozenset be needed? Skaperen 12 4,276 Apr-15-2020, 04:38 AM
Last Post: buran
  looking up docs for open file methods Skaperen 3 2,393 Nov-10-2019, 01:08 AM
Last Post: Skaperen
  Operator Precedence Direction in Docs blitz4 3 3,096 Mar-11-2018, 06:15 AM
Last Post: wavic
  Docs missing info? tozqo 3 4,388 Jun-21-2017, 06:50 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020