type of simple object

Jacek Generowicz jacek.generowicz at cern.ch
Tue Feb 1 06:18:17 EST 2005


"ajikoe at gmail.com" <ajikoe at gmail.com> writes:

> How do I know type of simple object is tuple or list or integer, for
> example my function should understand what is the object type passed in
> its argument


Answers ordered in decreasing degree of Pythonicity:

1) You are mistaken, the function almost certainly should not care
   whether the object is a tuple or a list (or integer)[1].

2) isinstance(obj, list) etc.

3) type(obj)



[1] You probably want to check whether the object is capable of doing
    whatever it is that you expect lists or tuples to do for you. For
    example: 


        def total(object):
            try:
                return sum(object) # The list-or-tuple case
            except TypeError:
                return object      # The integer case




More information about the Python-list mailing list