Skip to content

Instantly share code, notes, and snippets.

@dagbrown
Created July 31, 2014 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagbrown/dcd907f6348548632eb4 to your computer and use it in GitHub Desktop.
Save dagbrown/dcd907f6348548632eb4 to your computer and use it in GitHub Desktop.
Python WTF
Python 2.7.7 (default, Jul 19 2014, 21:44:39)
[GCC 4.8.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [ 1, 2, 3 ] + "hi there"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
>>> x = [ 1,2,3 ];x += "hi there"; x
[1, 2, 3, 'h', 'i', ' ', 't', 'h', 'e', 'r', 'e']
>>>
@wolever
Copy link

wolever commented Jul 31, 2014

Ya, the internal optimization of += to __iadd__ can have other fun implications… this one caused a bug in some of my code:

class Foo(object):
  def __init__(self):
    self._bar = []
  @property
  def bar(self):
    return self._bar

>>> x = Foo()
>>> x.bar += [1]
…
AttributeError: can't set attribute
>>> x.bar
[1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment