I have a codebase which uses StringType and NoneType(types module) in the python2.x codebase. On porting to Python3, tests failed as the types module in Python3.x does not have the above mentioned two types.
I solved the problem by replacing them with “str” and “None” respectively. I was wondering if there is another (right)way of doing this. What I’m doing currently definitely seems to work, but I’m doubtful.
Should I stick to the approach I have followed or is there something wrong in what I have done? If so, how do I correct it?
Checking None is usually done by calling
obj is None, while checking for string usually isisinstance(obj, str). In Python 2.x to detect both string and unicode, you could useisinstance(obj, basestring).If you use
2to3, it’s enough, but if you need to have single piece of code working in both Py2 and Py3, you may end up with something like that: