[ create a new paste ] login | about

Link: http://codepad.org/boee6PL6    [ raw code | output | fork ]

Python, pasted on Nov 23:
def is_builtin_type(typ):
  if type(typ) is type:
    try:
      return getattr(__builtins__, typ.__name__) == typ
    except AttributeError:
      pass
  return False

is_class = lambda typ: repr(typ).find("<class ") == 0

class Foo:
  pass

class Bar(object):
  pass

# Overrided str
class str(object):
  pass

# Copied int
int2 = int

check_ls = [(int, "int"), (Foo, "class Foo"), (Bar, "class Bar(object)"),
            (str, "Overrided str"), (int2, "Copied int")]

for typ, msg in check_ls:
  print msg
  print "  is_class:", is_class(typ)
  print "  is_builtin_type:", is_builtin_type(typ)
  print ""


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int
  is_class: False
  is_builtin_type: True

class Foo
  is_class: True
  is_builtin_type: False

class Bar(object)
  is_class: True
  is_builtin_type: False

Overrided str
  is_class: True
  is_builtin_type: False

Copied int
  is_class: False
  is_builtin_type: True



Create a new paste based on this one


Comments: