python中定义class的时候,有object和没有object的不同?例如:
class Solution(object):
class Solution():
这俩的区别在于——————
在python2.x中,通过分别继承自object和不继承object定义不同的类,之后通过dir()和type分别查看该类的所有方法和类型:
>>> class test(object): ... pass ... >>> dir(test) ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '_ _init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__size of__', '__str__', '__subclasshook__', '__weakref__'] >>> type(test) <type 'type'> >>> class test2(): ... pass ... >>> dir(test2) ['__doc__', '__module__'] >>> type(test2) <type 'classobj'>
在3.x中:两者是一致的,因为在3.x中,默认继承就是object了
>>> class test(object): pass >>> class test2(): pass >>> type(test) <class 'type'> >>> type(test2) <class 'type'> >>> dir(test) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] >>> dir(test2) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']