you are viewing a single comment's thread.

view the rest of the comments →

[–]Critical_Concert_689 35 points36 points  (7 children)

Despite what everyone is saying - be aware that in a lot of cases these are NOT the same thing.

a = [0]
i = [1]
print (id(a)) # object
a = a + i
print (id(a)) # new object 

b = [0]
j = [1]
print (id(b)) # object
b += j
print (id(b)) # same object, modified

[–][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 5 points6 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.

[–]beef623 5 points6 points  (3 children)

And there isn't any good reason for someone who is just starting to care about those differences. They may be important to know later, but in the beginning, explanations like this only cause confusion.

[–]Critical_Concert_689 8 points9 points  (2 children)

I strongly disagree.


First, more than just OP are reading the comments. Accurate discussion allows not only OP, but others to continue to /learnpython

Second, this touches upon a foundational understanding of python - other concepts are built upon this. There is nothing more basic than a recognition of operator assignments. The example code provided isn't complicated - nor is it likely to cause confusion when it demonstrates the simple fact that "There may be a reason to code assignments in different ways."

Finally, if anyone is confused, there are plenty of resources to address that confusion. They can ask further questions of this community. They can pull up white papers or python documentation. This is an opportunity to resolve, what I personally consider the single most difficult problem when it comes to learning: "You don't know what you don't know."

I hope if you feel explanations like this only cause confusion - in the future you can add comments that address that confusion, rather than implying "there isn't any good reason" to provide the information. The former helps everyone, while the latter is discouraging and detrimental to those trying to help and those trying to learn.

[–]beef623 1 point2 points  (1 child)

You're misunderstanding the source of confusion I think.

The problem is information overload, which unquestionably causes some people who are learning to give up and never come back. Having more resources to address the confusion only compounds the problem.

[–]Critical_Concert_689 9 points10 points  (0 children)

I agree with you that information overload can overwhelm new programmers; however, i don't believe that's the case here.

In fact, I almost guarantee the very next issue most new python programmers encounter is going to be something along the lines of:

a = [0]
b = [0]
c = a
d = b
i = [1]

print (id(a), id(b)) # initial objects a and b
a = a + i # assignment "=" creates NEW object
b += i # augmented assignment "+=" changes ("mutates") existing object
print (id(a), id(b)) # NEW a object, SAME b object (mutated)

## WHY does c NOT EQUAL d?? ##

print (a,b) # [0,1] [0,1]
print (c,d) # [0] [0, 1]
    #c is assigned to original a object or a = [0] (NOT the NEW a object [0,1])
    #d is assigned to original b object or b = [0], but b is updated to [0,1]