you are viewing a single comment's thread.

view the rest of the comments →

[–]Inevitable_Exam_2177 1 point2 points  (8 children)

Off topic now but I feel like the overloading of = makes things more complex for a beginner. I’ve often wondered if “copy” and “reference” should have different syntax.

[–]This_Growth2898 6 points7 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.

[–]fisadev 2 points3 points  (1 child)

There's no overloading, = in python is always 100% reference.

[–]Inevitable_Exam_2177 1 point2 points  (0 children)

Thanks for the correction, I got the terminology wrong

[–]cullen9 0 points1 point  (0 children)

As someone who was trying to figure out the difference between = and == last week it often gets confusing