isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Bug with `X` #216

Closed kckennylau closed 7 years ago

kckennylau commented 7 years ago

Please look at the following two codes:

[J*\]U5 3XJ0X@J0T5](http://pyth.herokuapp.com/?code=J%2a%5DU5+3XJ0X%40J0T5&debug=0) (The \] is not really there, but I cannot type bare ] in link text.)

JmU5 3XJ0X@J0T5

The only difference is how J is generated, but the end result is different.

Namely, in the first code, all the sub-arrays are replaced.

jakobkogler commented 7 years ago

This is not a bug related with X. It's just the way Python handles objects. Try this Python code:

>>> a = [[1, 2]] * 2
>>> a[0][0] = 100
>>> a
[[100, 2], [100, 2]]

Python doesn't actually copy elements, when you don't be explicit. It's the same thing as a = b. There is still only one object, not two. a and b simply point to the same object.

mU5 3 works though, since you generate three completely different lists.

We probably want to change the behavior of *. It should deepcopy the elements (like we do in = or ~).

kckennylau commented 7 years ago

"We"? How many developers are there?

kckennylau commented 7 years ago

Also, is there a golfier way to modify elements in 2D lists?