[Python-ideas] Proposal to change List Sequence Repetition (*) so it is not useless for Mutable Objects
Steven D'Aprano
steve at pearwood.info
Tue May 31 10:16:25 EDT 2016
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 ?
-1
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.
--
Steve
More information about the Python-ideas
mailing list