[Python-ideas] Proposal to change List Sequence Repetition (*) so it is not useless for Mutable Objects
Bar Harel
bzvi7919 at gmail.com
Tue May 31 15:11:03 EDT 2016
-1 as well.
I have used the generator multiplication many times before. The use of the
same object helps a lot in certain cases.
Changing that will cause a confusion among those who know the phenomenon
and would break compatibility.
Regarding your matrix example, maybe we can make use of the new '@'
operator for copying new objects, what do you say?
-- Bar Harel
On Tue, May 31, 2016 at 8:01 PM Terry Reedy <tjreedy at udel.edu> wrote:
> 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 ?
> >
> > -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.
>
> -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[1] = 4
> print(b[1])
> # 4 -- a surprise for those who expect '=' to copy the list object
>
> Sequence * is an elaboration of the same issue.
>
> --
> Terry Jan Reedy
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160531/7c34972c/attachment-0001.html>
More information about the Python-ideas
mailing list