Use this predicate to
test whether some instance
I
is an instance of some class
C
(or an
instance of any ancestor class from which C
inherits). The
general form:
isinstance(The second argument may be a class name, a type object, or a tuple of class names and type objects. If a tuple, the function will test the instance against each of the class names or type objects.I
,C
)
>>> class C1: ... pass ... >>> class C2(C1): ... pass ... >>> c2=C2() >>> isinstance(c2,C2) True >>> isinstance(c2,C1) True >>> isinstance(c2,int) False >>> isinstance(1,type(55)) True >>> isinstance(1, (int, float, long)) True >>> isinstance('Ni', (int, float, long)) FalseA most useful built-in Python class is
basestring
, which is the ancestor class of both
str
and unicode
types. It
is intended for cases where you want to test whether
something is a string but you don't care whether it is
str
or unicode
.
>>> isinstance(42, str) False >>> isinstance('x', str) True >>> isinstance(u'x', str) False >>> isinstance('x', basestring) True >>> isinstance(u'x', basestring) True
No comments:
Post a Comment