Wednesday, August 19, 2015

str, unicode and basestring - Python

Recently I faced one issue on Ubuntu 13.10 in python 2.7. I would like to share it here. the issue is regarding str and unicode type of strings.

The scenario is I am checking for the type of a input, as I am expecting  either string or dictionary (json object). So to know whether the input is string or dictionary, I am using python isinstance and checking for str and dict types. It was working fine in my local machine and failing in our dev environment for str type. The only difference between my local setup and dev envidonrment is Ubuntu OS version. In my local setup it was Ubuntu 13.10 and in Dev it is Ubuntu 14.04.

After struggling lot of hours finally found the issue is with isinstance for string data type. In Ubuntu 13.10 setup isinstance  is returning success for string type and in Ubuntu 14.04 it was not, because in 14.04 string data type becomes unicode and not str. I am just checking for str and not checking for unicode. To avoid this problem we can check for basestring it will work for str and unicode and OS version independent.

In python basestring is the parent for str and unicode. And unicode and str are siblings to each other. Check the below same python code.

str_ip = "checking for str type"
unicode_ip = u'checking for unicode type'

# checking for str type
if isinstance(str_ip, str):
    print "string type is str"
# checking for unicode type    
if isinstance(unicode_ip, unicode):
    print "string type unicode str"
# checking string  for base string 
if isinstance(str_ip, basestring):
    print "str_ip is a string"
# checking unicode for base string    
if isinstance(unicode_ip, basestring):
    print "unicode_ip is a string"

Happpy Coding!!!

Popular Posts