TypeErrors
suggest changeThese exceptions are caused when the type of some object should be different
TypeError: [definition/method] takes ? positional arguments but ? was given
A function or method was called with more (or less) arguments than the ones it can accept.
Example
If more arguments are given:
def foo(a): return a
foo(a,b,c,d) #And a,b,c,d are defined
If less arguments are given:
def foo(a,b,c,d): return a += b + c + d
foo(a) #And a is defined
return a += b + c + d is a SyntaxError, so this sample never reaches the TypeError for too few arguments.
Note: if you want to use an unknown number of arguments, you can use *args or **kwargs. See *args and **kwargs
TypeError: unsupported operand type(s) for [operand]: ‘???’ and ‘???’
Some types cannot be operated together, depending on the operand.
Example
For example: \+ is used to concatenate and add, but you can’t use any of them for both types. For instance, trying to make a set by concatenating (\+ing) 'set1' and 'tuple1' gives the error. Code:
set1, tuple1 = {1,2}, (3,4)
a = set1 + tuple1
Some types (eg: int and string) use both \+ but for different things:
b = 400 + 'foo'
Or they may not be even used for anything:
c = ["a","b"] - [1,2]
But you can for example add a float to an int:
d = 1 + 1.0
TypeError: ‘???’ object is not iterable/subscriptable:
An object is iterable if it has an __iter__ method that returns an iterator, and an iterator implements __next__. There is no __iterator__ method. CPython can also iterate an object with no __iter__ when it defines __getitem__ for indexes starting at zero, until that raises an IndexError.
Example
Here we are saying that bar is the zeroth item of 1. Nonsense:
foo = 1
bar = foo[0]
Here is another example. In this example for tries to iterate amount, but it can’t because amount is an int:
amount = 10
for x in amount: print(x)
TypeError: ‘???’ object is not callable
You are defining a variable and calling it later (like what you do with a function or method)
Example
foo = "notAFunction"
foo()