Ray's playground

 

Recipe 1.3. Testing Whether an Object Is String-like(Python Cookbook)

isAString
 1 >>> def isAString(anobj):
 2     return isinstance(anobj, basestring)
 3 
 4 >>> a = "hello"
 5 >>> isAString(a)
 6 True
 7 >>> b = 't'
 8 >>> isAString(b)
 9 True
10 >>> c = []
11 >>> isAString(c)
12 False
13 >>> class MyString(str):
14     pass
15 
16 >>> d = MyString()
17 >>> isAString(d)
18 True

 

posted on 2010-12-17 22:29  Ray Z  阅读(108)  评论(0编辑  收藏  举报

导航