• super()派生使用中的常见两个错误


    
    
    """
    super()派生可以继承父类的属性
    --super()派生继承父类的语法是:super().__init__()
    
    --super().__init__()中的__init__()这个括号是不用传值self的
    
    --super()的派生方法在Python2中是:super(自己的类名,self).__init__()
    
    --super()的属性查找顺序,严格按照属性的查找顺序来做
    先对象自己,然后再类,类中没有再到父类中找,父类中没有的话,才会报错。
    
    
    """
    #第一种错误
    class OldPerson:
        school='oldboy'
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
    
    class OldStudent():            #此处缺少要继承的父类,导致解释器运行到super()报错,添加OldPerson父类即可解决该问题。
    
        def __init__(self,name,age,sex,stu_id):
            super().__init__(name,age,sex)
            self.stu_id=stu_id
    
        def choose_course(self):
            print('%s is choosing course'%self.name)
            return 'woaini '
    
    stu1=OldStudent('luzhaoshan',25,'nan',2019)
    print(stu1.__dict__)
    print(stu1.choose_course())
    
    报错信息如下:
    Traceback (most recent call last):
      File "E:/Python   jieshiqi/9月份练习/9月6日/super()派生使用自己默写.py", line 32, in <module>
        stu1=OldStudent('luzhaoshan',25,'nan',2019)
      File "E:/Python   jieshiqi/9月份练习/9月6日/super()派生使用自己默写.py", line 25, in __init__
        super().__init__(name,age,sex)
    TypeError: object.__init__() takes no arguments
    
    
    #第二种错误
    
    """
    super()派生可以继承父类的属性
    --super()派生继承父类的语法是:super().__init__()
    
    --super().__init__()中的__init__()这个括号是不用传值self的
    
    --super()的派生方法在Python2中是:super(自己的类名,self).__init__()
    
    --super()的属性查找顺序,严格按照属性的查找顺序来做
    先对象自己,然后再类,类中没有再到父类中找,父类中没有的话,才会报错。
    
    
    """
    class OldPerson:
        school='oldboy'
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
    
    class OldStudent():
    
        def __init__(self,name,age,sex,stu_id):
            #super().__init__(name,age,sex)        #调试中把这行代码注释掉,终止继承父类,下面遇到choose_course函数时也会报错,因为print中的定义name属性,只定义了stu_id属性
            self.stu_id=stu_id
    
        def choose_course(self):
            print('%s is choosing course'%self.name)        #把name 调整为stu_id 属性即可解决该问题。
            return 'woaini '
    
    stu1=OldStudent('luzhaoshan',25,'nan',2019)
    print(stu1.__dict__)
    print(stu1.choose_course())
    
    报错信息如下:
    Traceback (most recent call last):
    {'stu_id': 2019}
      File "E:/Python   jieshiqi/9月份练习/9月6日/super()派生使用自己默写.py", line 34, in <module>
        print(stu1.choose_course())
      File "E:/Python   jieshiqi/9月份练习/9月6日/super()派生使用自己默写.py", line 29, in choose_course
        print('%s is choosing course'%self.name)
    AttributeError: 'OldStudent' object has no attribute 'name'
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    如何判断栈的增长方向
    时间复杂度
    shell基础part3
    shell基础part2
    shell基础part2
    linux基础part5
    linux基础part4
    linux基础part3
    linux基础part2
    shell基础part1
  • 原文地址:https://www.cnblogs.com/ludundun/p/11498010.html
Copyright © 2020-2023  润新知