Specifing arguments type for a function

Diez B. Roggisch deets at nospam.web.de
Tue Jun 20 06:57:22 EDT 2006


> What about
> def f(arg):
>     if type(arg)=='list':
>         #do something

Several things:

 - list is a string in your code - which wont work:

>>> type([]) == 'list'
False

It needs to be list w/o quotes.


  - you won't get sublclasses of list:

>>> class Foo(list):
...    pass
...
>>> type(Foo()) == list
False

So better use isinstance, as I already mentioned.

Diez



More information about the Python-list mailing list