On 5/31/2016 10:16 AM, Steven D'Aprano wrote:
On Tue, May 31, 2016 at 01:36:31PM +0000, Joseph Martinot-Lagarde wrote:
I can only agree here. Even today, despite knowing the fact, it's causing some headaches in some cases.
How about raising an exception if mutable objects are in the list ?
It's a gratuitous breakage that cannot solve the problem, because you cannot tell in advance which objects are mutable and which are not. The best you can do is recognise known built-ins.
("list is mutable, tuple is not, except when it contains a mutable item, but mymodule.MySequence may or may not be, there's no way to tell in advance.")
- maybe there are useful use cases of duplicating the reference ?
Absolutely. That's a standard way of grouping items taken from an iterable:
py> it = iter("hello world!") py> for a, b, c in zip(*[it]*3): # groups of three ... print(a, b, c) ... h e l l o w o r l d !
This wouldn't work if the iterators were three independent copies.
-1 also. As Steven has pointed out several times in other threads, Python is consistent in not copying objects unless requested. Thus
a = [1,2,3] b = a # a reference copy, not an object copy, as some expect a = 4 print(b) # 4 -- a surprise for those who expect '=' to copy the list object
Sequence * is an elaboration of the same issue.