Python: check argument type








  • for objects:

    def f(arg):
    if isinstance(arg, ClassA):
    print('A')
    elif isintance(arg, ClassB):
    print('B')
    elif issubclass(arg, ClassC):
    print('subclass of C')
    else:
    print("D")
  • for built-in types:

    def f(arg):
    if type(arg) is str:
    print "str"
    elif type(arg) is int:
    print "int"
    elif type(arg) is dict:
    print "dict"
    else:
    print "unsupported"



No comments:

Post a Comment