How to get python object type?

Python in fact has just a single object type: Object. And building on that adds __class__ property on any object. For instance on 'string' literal it will add the class str

s='one'
print(type(s)) # <class 'str'>
print (s.__class__) # <class 'str'>

To check Python object type on a list you have to use type function again.

l=['one','two']
type(l) # list
print (l.__class__) # <class 'list'>

When you need to check you can do it in this pythonic way:

l=[]
if type(l).__name__== 'list': 
  print('Is a list!')
else: 
  print('Not a list')

tags: & category: -