List replication operator

Peter Otten __peter__ at web.de
Sat May 26 09:57:05 EDT 2018


Steven D'Aprano wrote:

> On Fri, 25 May 2018 09:28:01 +0200, Peter Otten wrote:
> 
>> Yet another arcanum to learn for beginners with little return. If you
>> cannot refrain from tinkering with the language at least concentrate on
>> the features with broad application. Thank you.
> 
> Broader than multi-dimensional arrays? There are a bazillion uses for
> them. How many do you need before it is "broad application"?
> 
> https://www.google.com/search?q=multidimensional+arrays

Sorry, I don't see how your suggestion helps with multidimensional arrays 
which aren't even part of Python's stdlib.

In practice newbies will be told to replace

bins = [[]] * N

with 

bins = [[]] ** N

just as they are now told to replace it with

bins = [[] for dummy in range(N)]

Someone showing up with

[[[[0]*10]*10]*10]*10

should certainly be informed about numpy.

> https://www.google.com/search?q=what+are+some+uses+for+multidimensional
> +arrays
> 
> "My programming language doesn't support arithmetic, because I only
> provide features with broad application. Arithmetic is only useful for
> manipulating numbers."
> 
> 
> Beginners already learn list * operator, and get *extremely emotional*

I don't think there is a significant correlation between getting emotional 
and being right*.

> when it doesn't copy lists like they expect:
> 
> https://bugs.python.org/issue33636
> 
> This is a frequent, recurring pain point. Experienced programmers forget
> how confusing the behaviour of * is because they're so used to the
> execution model. They forget that writing a list comp is not even close
> to obvious, not only for beginners but even some experienced Python
> programmers.

It may be unexpected, just as

animal = Animal()

dog = animal
dog.noise = "woof"

cat = animal
cat.noise = "miaou"

or

def augmented(item, items=[]):
    items.append(item)
    return items

a = augmented(10)
b = augmented(20)
assert a == [10]
assert b == [20]

is if your mental model operates with values rather than references.


(*) Perhaps we should try to repeat the success of

from __future__ import braces

with

from __future__ import value_semantics





More information about the Python-list mailing list