A..1 クラス,型の判定

#!/opt/local/bin/python2

import numpy
import types

def foo(x):
  """ foo """
  if isinstance(x, float):
    print 'float'
  elif isinstance(x, complex):
    print 'complex'
  elif isinstance(x, int):
    print 'int'
  elif isinstance(x, list):
    print 'list'
  elif isinstance(x, str):
    print 'string'
  elif isinstance(x, numpy.ndarray):
    if isinstance(x[0], types.FloatType):
      print 'float array'
    elif isinstance(x[0], types.IntType):
      print 'int array'
    elif isinstance(x[0], types.ComplexType):
      print 'complex array'
    else:
      print('are')
  else:
    print 'error'

if __name__ == "__main__":
  foo(1)
  foo(1.0)
  foo(1.0+2.0*1j)
  foo([1,2,3])
  foo('Hello')
  foo(numpy.array([1,2,3]))
  foo(numpy.array([1+1j,2+2j,3+3j]))

桂田 祐史
2018-01-07