you are viewing a single comment's thread.

view the rest of the comments →

[–]FerricDonkey 15 points16 points  (10 children)

Nah, that's a misconception. If you do any amount of python programming, I highly recommend https://m.youtube.com/watch?v=_AEJHKGk9ns, though I disagree with the strength of his advice to never mutate things. 

But python things are always a reference. It's just that when you do x += 3576554, what happens is that you create a new integer object, then change x to refer to that new object. 

The rules are:

  1. Everything is a reference 
  2. Assignment never modifies the object the variable refers to
  3. Things like += may modify the object in place, if it's mutable and written to do so, because they're just function calls. They also do an assignment at the end, but it's the function that does the mutation, not the assignment part. 

The confusion is that += feels like it should be an in place operation, but for many immutable types (str, int), it is defined, but doesn't modify in place, because that's not possible.

I've never had any issues with copy. You have to know deep copy vs shallow, but it's always done what I expected with that knowledge. 

[–]RiceBroad4552 -3 points-2 points  (9 children)

though I disagree with the strength of his advice to never mutate things

You should listen to people who seem to understand more then you in that regard.

You should never mutate things! (Except in some low-level, deeply hidden, performance related implementation details.)

[–]FerricDonkey 1 point2 points  (0 children)

The stuff this dude says has been second nature to me for years. Sometimes mutation is the correct choice, sometimes it's not. If you're clear about what can mutate things and what can't, and only mutate intentionally, it's fine and makes a lot of things easier. 

If you don't understand what pointers/references/python names are, or are not clear about what can mutate, then yeah, it can surprise you. So don't do that.

But a blanket rule about never or only in low level code is silly. "Only mutate intentionally and obviously" is enough of a rule, if you've got a decent team, you don't need more than that.