List replication operator

bartc bc at freeuk.com
Fri May 25 13:13:40 EDT 2018


On 25/05/2018 17:58, Rob Gaddi wrote:

> So, in the spirit of explicit being better than implicit, please assume 
> that for actual implementation replicate would be a static method of 
> actual list, rather than the conveniently executable hackjob below.
> 
> _list = list
> _nodefault = object()
> 
> class list(_list):
>    @staticmethod
>    def replicate(*n, fill=_nodefault, call=list):

That seems to work, but the dimensions are created in reverse order to 
what I expected. Which is to have the order of indices corresponding to 
the order of dimensions. So:

  x=list.replicate(2,3,4)

  print (len(x))
  print (len(x[0]))
  print (len(x[0][0]))

Gives output of 4, 3, 2 rather than 2, 3, 4.

Which means that x[0][0][3] is a bounds error.

-- 
bartc



More information about the Python-list mailing list