A consistent way to check types in python
Question: I'm finding that type checking in python (pre py3, I'm not sure about the
state in py3) is bit of a mess, here's what I mean:

First, there are the built in types (int, str etc), and the types defined in
the 'types' module, where you have to import the types module and use it as a
prefix. You can jump this hurdle easily if you get a 'name undefined error'
for the type you're using, and try the types in the types module.

Then, there are the names printed out by the type() function that you can't
figure out what type they correspond to - examples:

    
    
    In [38]: type(Session.__dict__['mix']) 
    Out[38]: staticmethod
    
    In [39]: type(Session.__dict__['mix']) == staticmethod
    Out[39]: True
    

Ok, so this one is a built-in type. But then:

    
    
    In [47]: type(Session.__dict__['Cuser']) 
    Out[47]: classobj
    
    In [48]: type(Session.__dict__['Cuser']) == classobj
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-48-b062b09548fb> in <module>()
    ----> 1 type(Session.__dict__['Cuser']) == classobj
    
    NameError: name 'classobj' is not defined
    
    In [49]: type(Session.__dict__['Cuser']) == types.classobj
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-49-2a88e0b8e176> in <module>()
    ----> 1 type(Session.__dict__['Cuser']) == types.classobj
    
    AttributeError: 'module' object has no attribute 'classobj'
    

'classobj' is not mentoned in the types module
[documentation](http://docs.python.org/2/library/types.html), but after some
search, I find out it corresponds to `types.ClassType`:

    
    
    In [50]: type(Session.__dict__['Cuser']) == types.ClassType
    Out[50]: True
    

Seeing from the above examples that type checking is not so straight forward,
my question is: is there a more consistent way to check types out there that
I'm not aware of?


Answer: python stresses duck-typing:

> if it walks like a duck and quacks like a duck, it's a duck.

Instead of focusing on type, focus on attributes. For example, if
hasattr(a,'next') (`hasattr(a,'__next__')` in python 3), then you know `a` can
be treated as an iterable.

