============= Python Errors ============= .. code-block:: python if true: print 1 :: File "tmp.py", line 2 print 1 ^ IndentationError: expected an indented block .. code-block:: python if true: print 1 print 2 :: File "tmp.py", line 3 print 2 ^ IndentationError: unindent does not match any outer indentation level .. code-block:: python # -*- encoding: utf-8 -*- if true:  print 1 # unicode space character :: File "tmp.py", line 3  print 1 # unicode space character ^ SyntaxError: invalid syntax .. code-block:: python print "こんにちは" :: File "tmp.py", line 1 SyntaxError: Non-ASCII character '\xe3' in file tmp.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details .. code-block:: python # -*- encoding: utf-8 -*- print "こんにちは" :: こんにちは .. code-block:: python def foo(): pass foo(1) :: Traceback (most recent call last): File "tmp.py", line 3, in foo(1) TypeError: foo() takes no arguments (1 given) .. code-block:: python def foo(x=1): pass foo(y=1) :: Traceback (most recent call last): File "tmp.py", line 3, in foo(y=1) TypeError: foo() got an unexpected keyword argument 'y' .. code-block:: python def foo(x, y, z=0): pass foo(1, z=1) :: Traceback (most recent call last): File "tmp.py", line 3, in foo(1, z=1) TypeError: foo() takes at least 2 arguments (2 given) .. code-block:: python def foo(x=0, **kw): print x, kw foo(1, x=2) :: Traceback (most recent call last): File "tmp.py", line 4, in foo(1, x=2) TypeError: foo() got multiple values for keyword argument 'x' .. code-block:: python ''[0] :: Traceback (most recent call last): File "tmp.py", line 1, in ''[0] IndexError: string index out of range .. code-block:: python '' + 0 :: Traceback (most recent call last): File "tmp.py", line 1, in '' + 0 TypeError: cannot concatenate 'str' and 'int' objects .. code-block:: python 0 + '' :: Traceback (most recent call last): File "tmp.py", line 1, in 0 + '' TypeError: unsupported operand type(s) for +: 'int' and 'str' .. code-block:: python -'' :: Traceback (most recent call last): File "tmp.py", line 1, in -'' TypeError: bad operand type for unary -: 'str' .. code-block:: python x :: Traceback (most recent call last): File "tmp.py", line 1, in x NameError: name 'x' is not defined .. code-block:: python u'' + '\xFF' :: Traceback (most recent call last): File "tmp.py", line 1, in u'' + '\xFF' UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128) .. code-block:: python {}['x'] :: Traceback (most recent call last): File "tmp.py", line 1, in {}['x'] KeyError: 'x' .. code-block:: python [].x :: Traceback (most recent call last): File "tmp.py", line 1, in [].x AttributeError: 'list' object has no attribute 'x' .. code-block:: python import x :: Traceback (most recent call last): File "tmp.py", line 1, in import x ImportError: No module named x .. code-block:: python int("a") :: Traceback (most recent call last): File "tmp.py", line 1, in int("a") ValueError: invalid literal for int() with base 10: 'a' .. code-block:: python "%s %s %s" % (1, 2) :: Traceback (most recent call last): File "tmp.py", line 1, in "%s %s %s" % (1, 2) TypeError: not enough arguments for format string .. code-block:: python "%s %s" % [1, 2] :: Traceback (most recent call last): File "tmp.py", line 1, in "%s %s" % [1, 2] TypeError: not enough arguments for format string .. code-block:: python 1 / 0 :: Traceback (most recent call last): File "tmp.py", line 1, in 1 / 0 ZeroDivisionError: integer division or modulo by zero .. code-block:: python def foo(): return foo() + 1 :: Traceback (most recent call last): File "tmp.py", line 4, in foo() + 1 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' .. code-block:: python class Foo(object): def say(self): pass Foo.say() :: Traceback (most recent call last): File "tmp.py", line 5, in Foo.say() TypeError: unbound method say() must be called with Foo instance as first argument (got nothing instead) .. code-block:: python class A(object): pass class B(object): pass class AB(A, B): pass class BA(B, A): pass class C(AB, BA): pass :: Traceback (most recent call last): File "tmp.py", line 5, in class C(AB, BA): pass TypeError: Error when calling the metaclass bases Cannot create a consistent method resolution order (MRO) for bases B, A .. code-block:: python def func(a=[]): a.append(1) return a print func() print func() print func() :: [1] [1, 1] [1, 1, 1] .. code-block:: python def f(): print "called" def func(x=f()): pass :: called .. code-block:: python # assignment in bar doesn't affect outside def foo(): x = 0 def bar(): x = 1 bar() print x foo() :: 0 .. code-block:: python # list comprehension doesn't make scope funcs = [(lambda: x) for x in range(3)] print funcs[0]() print funcs[1]() :: 2 2 .. code-block:: python x = 0 def foo(): x += 1 foo() :: Traceback (most recent call last): File "tmp.py", line 5, in foo() File "tmp.py", line 3, in foo x += 1 UnboundLocalError: local variable 'x' referenced before assignment .. code-block:: python x = 0 def foo(): global x x += 1 foo() print x :: 1 .. code-block:: python # class variable is not a global variable class Foo(object): x = 1 def foo(self): print x Foo().foo() :: Traceback (most recent call last): File "tmp.py", line 7, in Foo().foo() File "tmp.py", line 5, in foo print x NameError: global name 'x' is not defined .. code-block:: python class Foo(object): x = 1 def foo(self): print self.x Foo().foo() :: 1 .. code-block:: python # class variable is shared by instances class Foo(object): x = [] def foo(self): self.x.append(1) print self.x Foo().foo() Foo().foo() :: [1] [1, 1] .. code-block:: python def foo(a=1, b): pass :: File "tmp.py", line 1 def foo(a=1, b): pass SyntaxError: non-default argument follows default argument .. code-block:: python def foo(a=1, b): pass :: File "tmp.py", line 1 def foo(a=1, b): pass SyntaxError: non-default argument follows default argument .. code-block:: python object.a = 1 :: Traceback (most recent call last): File "tmp.py", line 1, in object.a = 1 TypeError: can't set attributes of built-in/extension type 'object' .. code-block:: python class MetaFoo(type): pass class MetaBar(type): pass class Foo(object): __metaclass__ = MetaFoo class Bar(Foo): __metaclass__ = MetaBar :: Traceback (most recent call last): File "tmp.py", line 6, in class Bar(Foo): TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases .. code-block:: python class Foo(object): @property def foo(self): return self.foo f = Foo() f.foo = 1 :: Traceback (most recent call last): File "tmp.py", line 7, in f.foo = 1 AttributeError: can't set attribute .. code-block:: python while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: while 1: print "hello" :: SystemError: too many statically nested blocks .. code-block:: python return :: File "tmp.py", line 1 return SyntaxError: 'return' outside function