源码:
class TestBaseClassFather(): def __init__(self,name,age): self.name = name self.age = age class TestBaseClassExtendChild(TestBaseClassFather): def __init__(self,name,age,major): super(TestBaseClassExtendChild, self).__init__(name,age) self.major = major
此时报错:
bash-3.2$ python base_1/3_class.py Traceback (most recent call last): File "base_1/3_class.py", line 175, in <module> testMulitExtend() File "base_1/3_class.py", line 164, in testMulitExtend basChild = TestBaseClassExtendChild("zhangsan",23,"shuxue") File "base_1/3_class.py", line 153, in __init__ super(TestBaseClassExtendChild, self).__init__(name,age) TypeError: must be type, not classobj
接着写上object:
class TestBaseClassFather(object): ''' 此处必须写上继承自object ''' def __init__(self,name,age): self.name = name self.age = age class TestBaseClassExtendChild(TestBaseClassFather): def __init__(self,name,age,major): super(TestBaseClassExtendChild, self).__init__(name,age) self.major = major
执行结果正常:
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'major', 'name'] <class '__main__.TestBaseClassExtendChild'> True {'age': 23, 'name': 'zhangsan', 'major': 'shuxue'}
代沟000
相关分类