基本类

  1. class A(object):
  2. age = 10
  3. def add(self, num):
  4. self.age += num
  5. # type方法
  6. def add(self, num):
  7. self.age += num
  8. A = type('A', (), {'age': 10, 'add': add})
  1. def __init__(self, *args, **kwargs):
  2. self.args = args
  3. self.kwargs = kwargs
  4. C = type('C', (), {'__init__': __init__})

继承类

  1. class A(object):
  2. age = 10
  3. def add(self, num):
  4. self.age += num
  5. class B(A):
  6. def get_age(self):
  7. return self.age
  8. # type方法
  9. def add(self, num):
  10. self.age += num
  11. A = type('A', (), {'age': 10, 'add': add})
  12. B = type('B', (A,), {'get_age': lambda self: self.age})