Checking an Object’s type

In the following example, the same function is called with an integer, a floating point value, and a string.

def function(value):
      print value
if __name__=="__main__":

The Type function allows you to check what type a variable has. This funcation returns a type descriptor, which is unique object for each type provided by python interpreter

def dump(value):
             print type(value),value
if __name__=="__main__":

<type ‘int’> 1
<type ‘float’> 1.0
<type ‘str’> one

Each type has a single corresponding type object, which means that you can use the is operator to do type testing like.

if isinstance(file, type(" "):
           __ code__

The callable function checks if an object can be called.It returns true for functions, methos, lambda expression, classes, and class instance that define the __call__ method.

if callable(function):
        print funcation, "is callable"
       print function, "is not callable"

You can use the isinstance function which checks if an object is an instance of a given classs like

if isinstance(object,classname):

Recommended Posts

Leave a Reply

Your email address will not be published. Required fields are marked *