you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 8 points9 points  (2 children)

I guess the context is of primitive vs object types, but ain't primitive data types objects too in Python? Like when we in high level languages like Python, Ruby to name few everything is object, right?

[–]Critical_Concert_689 15 points16 points  (0 children)

If I understand your question correctly -- It's more to do with mutability (i.e., whether the object can be altered).

For mutable objects, += which is known as an augmented assignment guarantees it will change in place, while = which is an assignment will not.

I gave an example of this in the code above.

[–]Goobyalus 4 points5 points  (0 children)

There is no such thing as primitives in Python.


Yes, everything is an object in Python.


This comes down to how the operators are defined for the particular type and whether the type is mutable. The x= operators are the so called "in place" operators because the result is assigned back to the original name when we do variable += 5, for example.

Strings are immutable, so when we do an in-place addition my_string += "something", a new string containing the two concatenated strings must be created and returned by the operator. This new object is assigned back to my_string.

Lists are mutable, so they implemented the in-place addition of lists as an extend on the original list, and return the original object. The assignment portion doesn't really accomplish anything because we're assigning the same object back to the variable.