Python is a dynamically typed language, which means that a given
variable can be bound to values of different types at different
occasions. In the following example, the same function is called with
an integer, a floating point value, and a string:
def function(value):
print value
function(1)
function(1.0)
function("one")
The type function allows you to check what type a variable has.
This function returns a type descriptor , which is a unique object for
each type provided by the Python interpreter.
Example: Using the type function
# File: `builtin-type-example-1.py <builtin-type-example-1.py>`__
def dump(value):
print type(value), value
dump(1)
dump(1.0)
dump("one")
Each type has a single corresponding type object, which means that you
can use the is operator (object identity) to do type testing:
Example: Using the type function to distinguish between file names
and file objects
# File: `builtin-type-example-2.py <builtin-type-example-2.py>`__
def load(file):
if isinstance(file, type("")):
file = open(file, "rb")
return file.read()
print len(load("samples/sample.jpg")), "bytes"
print len(load(open("samples/sample.jpg", "rb"))), "bytes"
4672 bytes
4672 bytes
The callable function checks if an object can be called (either
directly or via apply). It returns true for functions, methods,
lambda expressions, classes, and class instances which define the
__call__ method.
Example: Using the callable function
# File: `builtin-callable-example-1.py <builtin-callable-example-1.py>`__
def dump(function):
if callable(function):
print function, "is callable"
else:
print function, "is *not* callable"
class A:
def method(self, value):
return value
class B(A):
def __call__(self, value):
return value
a = A()
b = B()
dump(0) # simple objects
dump("string")
dump(callable)
dump(dump) # function
dump(A) # classes
dump(B)
dump(B.method)
dump(a) # instances
dump(b)
dump(b.method)
0 is *not* callable
string is *not* callable
is callable
is callable
A is callable
B is callable
is callable
is *not* callable
** is callable
is callable
**
Note that the class objects ( A and B) are both callable; if
you call them, they create new objects. However, instances of class
A are not callable, since that class doesn’t have a __call__
method.
You’ll find functions to check if an object is of any of the built-
in number, sequence, or dictionary types in the `operator
<operator.htm>`__ module. However, since it’s easy to create a
class that implements e.g. the basic sequence methods, it’s usually
a bad idea to use explicit type testing on such objects.
Things get even more complicated when it comes to classes and
instances. Python doesn’t treat classes as types per se. Instead,
all classes belong to a special class type, and all class instances
belong to a special instance type.
This means that you cannot use type to test if an instance belongs
to a given class; all instances have the same type! To solve this, you
can use the isinstance function, which checks if an object is an
instance of a given class (or of a subclass to it).
Example: Using the isinstance function
# File: `builtin-isinstance-example-1.py <builtin-isinstance-example-1.py>`__
class A:
pass
class B:
pass
class C(A):
pass
class D(A, B):
pass
def dump(object):
print object, "=>",
if isinstance(object, A):
print "A",
if isinstance(object, B):
print "B",
if isinstance(object, C):
print "C",
if isinstance(object, D):
print "D",
print
a = A()
b = B()
c = C()
d = D()
dump(a)
dump(b)
dump(c)
dump(d)
dump(0)
dump("string")
=> A
** => B
=> A C
=> A B D
0 =>
string =>
**
The issubclass is similar, but checks whether a class object is
the same as a given class, or is a subclass of it.
Note that while isinstance accepts any kind of object, the
issubclass function raises a TypeError exception if you use it
on something that is not a class object.
Example: Using the issubclass function
# File: `builtin-issubclass-example-1.py <builtin-issubclass-example-1.py>`__
class A:
pass
class B:
pass
class C(A):
pass
class D(A, B):
pass
def dump(object):
print object, "=>",
if issubclass(object, A):
print "A",
if issubclass(object, B):
print "B",
if issubclass(object, C):
print "C",
if issubclass(object, D):
print "D",
print
dump(A)
dump(B)
dump(C)
dump(D)
dump(0)
dump("string")
A => A
B => B
C => A C
D => A B D
0 =>
Traceback (innermost last):
File "builtin-issubclass-example-1.py", line 29, in ?
File "builtin-issubclass-example-1.py", line 15, in dump
TypeError: arguments must be classes