Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I mean, []*2 copies the indices, hence the references in the case of arrays. Indices must be of finite length, so arrays probably don't contain other arrays.
-
This is a nice example of why basic collections like arrays should be value types.
In most languages they are reference types and it sucks. -
@Lensflare In all languages that guarantee tolerable speed and memory safety, all variables are fixed length. Eg. in C/C++ you can have an int[20] as a value type, but you definitely can't have an int[], because it doesn't have a size and hence can't be allocated. So if you want to pass around int[]-s, as you probably do, you'll have to use a pointer, which is of fixed size.
In python it isn't a pointer but rather some structure, but that structure still can't store the value of the array, only a reference to it, as it still needs to be of fixed size. -
Also, strings are often made immutable through an abstraction layer, but they're still reference types. You shouldn't copy arbitrary arrays when passed or stored because arrays can get very large.
-
@Lor-inc in order to optimize for speed, value type collections are implemented with copy-on-write semantics. And with value types, the question of mutability is not about the type but about the declaration of the value.
Together, you get nearly perfect speed, safety and convenience. -
And in the rare cases when you really need to pass a collection by reference, all languages have some kind of syntax for that.
But it doesn't go the other way. You just can't pass a reference type collection by value. You'll need to write some custom copy code.
Related Rants
Fuck me sideways, it took me so long to figure out what caused a certain bug. Thanks python
>>> list = [[0] * 2] * 2
>>> list
[[0, 0], [0, 0]]
>>> list[0][0] = 1
>>> list
[[1, 0], [1, 0]]
rant
python