Pythonコード最適化基盤——判断対象タイプ



# -*- coding: utf-8 -*-
#


# def displayNumType(num):
#     print num, 'is',
#     if type(num) == type(0):
#         print 'an integer'
#     elif type(num) == type(0L):
#         print 'a long'
#     elif type(num) == type(0.0):
#         print 'a float'
#     elif type(num) == type(0+0j):
#         print 'a complex number'
#     else:
#         print 'not a number at all!!'
# 
# 
# #         
# #             type()  ,    types         
# import types
# 
# if type(num) == types.IntType:
#     pass
# 
# #             
# #   :
# if type(num) == type(0):
#     pass
# #      :
# if type(num) is types.IntType:
#     pass
# 
# #      
# #import types
# from types import IntType
# if type(num) is IntType:
#     pass
# 
# #          :  isinstance()
# 
# 
#     
def displayNumType(num):
    print num ,'is',
    #isinstance           
    #          
    if isinstance(num,(int, long, float, complex)):
        #    type   
        print 'a number of type:', type(num).__name__
    else:
        print 'not a number at all!!'

displayNumType(-69)
displayNumType(9999999999999999999999999L)
displayNumType(98.6)
displayNumType(-5.2+1.9j)
displayNumType('xxx')