Cameron Hiruta Cameron Hiruta - 3 months ago 7
Python Question

Why does python give me a TypeError for this function? (new to coding)

http://i.stack.imgur.com/IB0Yq.png

I'm new to coding and not sure why the list is being typed as a tuple.

Answer

You've used the same name, list, for both a built-in type and a local variable. Don't re-use the built-in names. Quoting PEP-8:

If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss . (Perhaps better is to avoid such clashes by using a synonym.)

Try:

def funct2(list_):
    if type(list_) == list:
        ...

Or, better:

def funct2(list_):
    if isinstance(list_, list):
        ...