It is always better to use isinstance(x, y) instead of type(x) == y .
Since everything is an object in Python, isinstance(attr, object) does not make sense, because (I think) it always returns true.
Itβs best to blacklist certain types. For example, you check if it goes further than int, float, str, unicode, list, dict, set, ... , otherwise you just print it.
For example:
def dump(obj, level=0): for a in dir(obj): val = getattr(obj, a) if isinstance(val, (int, float, str, unicode, list, dict, set)): print level*' ', val else: dump(val, level=level+1)
UPDATE : isinstance takes into account inheritance, so if you try to see if the object is an instance of the parent class, it will return True, or maybe not when using the type.
Since in this case you will test primitive types, in this case it may not make any difference, but in general isinstance preferable.
Check out this example:
>>> class A(object): pass ... >>> class B(A): pass ... >>> a, b = A(), B() >>> type(a) <class '__main__.A'> >>> type(a) == A True >>> type(b) <class '__main__.B'> >>> type(b) == B True >>> type(b) == A False >>>
You can check the documents