[Python-ideas] Proposal to change List Sequence Repetition (*) so it is not useless for Mutable Objects
Lorenz
code at lorenzquack.de
Tue May 31 07:39:47 EDT 2016
Matthew Tanous <mtanous22 at ...> writes:
> It is my opinion that the sequence repetition operator should be
> modified to make copies of the objects it is repeating, rather than
> copying references alone. I believe this would both be more intuitive
> from a semantic point of view and more useful for the developer.
>
> This would change the operator in a way that is mostly unseen in current
> usage ([5] * 3 would still result in [5, 5, 5]) while treating mutable
> nesting in a way that is more understandable from the apparent intent of
> the syntax construction.
>
Hi Matthew,
I agree that when I was starting out with python this tripped me up a couple
of times but Marc-Andre pointed out it is not necessarily easy to solve in a
generic way.
As a point of reference, I currently tend to do this via generators
arr = list(Foo() for i in range(5))
or list comprehensions
arr = [Foo() for i in range(5)]
note that this works for the mutable and immutable case.
Kind Regards,
Lorenz
More information about the Python-ideas
mailing list