Tip

如果一个类不继承自其它类, 就显式的从object继承. 嵌套类也一样.

  1. Yes: class SampleClass(object):
  2. pass
  3.  
  4.  
  5. class OuterClass(object):
  6.  
  7. class InnerClass(object):
  8. pass
  9.  
  10.  
  11. class ChildClass(ParentClass):
  12. """Explicitly inherits from another class already."""
  1. No: class SampleClass:
  2. pass
  3.  
  4.  
  5. class OuterClass:
  6.  
  7. class InnerClass:
  8. pass

继承自 object 是为了使属性(properties)正常工作, 并且这样可以保护你的代码, 使其不受 PEP-3000 的一个特殊的潜在不兼容性影响. 这样做也定义了一些特殊的方法, 这些方法实现了对象的默认语义, 包括 new, init, delattr, getattribute, setattr, hash, repr, and str .