Skip to content

Another WAT Moment, Python Style

You have probably seen the famous WAT video from Destroy All Software. If you haven’t seen the video, it is a MUST.

As part of a recent StackOverflow letter, I saw that Python should also be part of that presentation. The question is, “what is the difference between i += x and i = i + x in Python. It should be the same thing right? Well, not exactly. From the example shown in the answers, when using arrays and i += x:

a = [1, 2, 3]
b = a
b += [1, 2, 3]
print a  #[1, 2, 3, 1, 2, 3]
print b  #[1, 2, 3, 1, 2, 3]

whereas is you use i = i + x:

a = [1, 2, 3]
b = a
b = b + [1, 2, 3]
print a #[1, 2, 3]
print b #[1, 2, 3, 1, 2, 3]
WAT?

Here’s the link the original question on stackoverflow. Language designers never stop amazing me.

Enhanced by Zemanta
Published inThoughts

Be First to Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.