For my program I have a lot of places where an object can be either a string or a list containing strings and other similar lists. These are generally read from a JSON file. They both need to be treated differently. Right now, I am just using isinstance, but that does not feel like the most pythonic way of doing it, so does anyone have a better way of doing it?
views:
177answers:
4
A:
You can use types module:
import types
if type(ls) == types.ListType:
#your code for list objects here
Mustafa Zengin
2010-07-12 10:17:34
The preferred way of comparing types is by using `isinstance` or otherwise with the `is` operator. http://docs.python.org/library/types.html
WoLpH
2010-07-12 10:20:36
Wouldn't just "type(ls) == list" work just as well?
Lars Wirzenius
2010-07-12 10:22:02
You can use `type(ls) is list` however that won't work for subclasses of list
gnibbler
2010-07-12 10:25:49
@Lars Wirzenius: No. That attempts to invoke a comparison of some kind, and while the default comparison for objects of type `type` is to compare object ids, and this results in identical behavior to `is`, it's not guaranteed. In this case `is` or preferably `isinstance` is the right answer.
Omnifarious
2010-07-12 10:26:49
+2
A:
Using isinstance
:
On Python>=2.3 a string may be a str
or unicode
type. To check both cases:
if isinstance(a,basestring): # same as isinstance(obj, (str, unicode))
print "Object is a string"
From Python 3 only one string type exists, so instead of basestring
you should use str
:
if isinstance(a,str):
print "Object is a string"
zoli2k
2010-07-12 10:22:52
+7
A:
No need to import modules, isinstance()
and basestring
(versions before 3, str
and unicode
for all versions including 3) will do the job for you:
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> isinstance(u'', (str, unicode))
True
>>> isinstance('', (str, unicode))
True
>>> isinstance([], (str, unicode))
False
>>>
From PEP008:
Object type comparisons should always use
isinstance()
instead of comparing types directly.
Johnsyweb
2010-07-12 10:23:40
People should be discouraged from using basestring nowadays because it doesn't exist in Python 3.
Omnifarious
2010-07-12 10:24:08
Is there pythonic way of doing this in Python 2 that will work in Python 3? `isinstance(u'', str)` in Python 2 returns `False`.
Johnsyweb
2010-07-12 10:32:07
@Johnsyweb: you could emulate this behaviour in Python 3 with `basestring = str`. But that's about it I suppose.
WoLpH
2010-07-12 10:44:23
The second argument to `isinstance` can be a tuple, so this would be better than checking against `basestring`: `isinstance(u'', (str, unicode))`
Jesse Dhillon
2010-08-15 04:16:54
+5
A:
Since Python3 no longer has unicode
or basestring
, in this case ( where you are expecting either a list or a string) it's better to test against list
if isinstance(thing, list):
# treat as list
else:
# treat as str/unicode
as that is compatible with both Python2 and Python3
gnibbler
2010-07-12 10:30:53