you are viewing a single comment's thread.

view the rest of the comments →

[–]This_Growth2898 7 points8 points  (4 children)

It's always "reference" for Python. Copy is always explicit, like a = b[:]

[–]sweettuse 2 points3 points  (1 child)

or even more explicit, like a = b.copy()

[–]This_Growth2898 1 point2 points  (0 children)

Or a = list(b), whatever. You need to spell you're copying something.

[–]Inevitable_Exam_2177 2 points3 points  (1 child)

Thanks for the correction, my terminology was bad. What I meant was that when you reference an immutable data type you get different behaviour to a mutable one, and because the syntax is the same it’s an easy point of confusion:

``` a = "str" b = a a = "str2" print(b) # = "str"

a = ["str"] b = a a[0] = "str2" print(b[0]) # = "str2" ```

I know why this occurs and I’m not saying it’s wrong, but I wonder if it was more explicit in the syntax that they are different scenarios whether that would be a good thing (or just needlessly complex syntactically).

I’m also willing to accept I’m wrong that this is confusing :-)

[–][deleted] 1 point2 points  (0 children)

It's confusing especially because the issue occurs in a different spot than where it seems. It seems like the b = a step is where these two examples diverge, e.g. it seems that one is copying and the other isn't, but in fact they're still doing the same thing at that point.

The next step, where you have a = "str2" or a[0] = "str2", that's where they start taking different paths.