zoukankan      html  css  js  c++  java
  • python初始化父类错误

    源码如下:

    #!/usr/bin/env python
    
    class Bird():
            def __init__(self):
                    self.hungry = True
    
            def eat(self):
                    if self.hungry:
                            print 'Aaaah...'
                            self.hungry = False
                    else:
                            print 'No, Thanks!'
    
    
    class SongBird(Bird):
            def __init__(self):
                    super(SongBird, self).__init__()
           #       Bird.__init__(self)
                    self.sound = 'Squawk!'
    
            def sing(self):
                    print self.sound

    错误信息如下:

    >>> from Bird import Bird, SongBird
    >>> b = Bird()
    >>> b.eat()
    Aaaah...
    >>> b.eat()
    No, Thanks!
    >>> sb = SongBird()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "Bird.py", line 17, in __init__
        super(SongBird, self).__init__()
    TypeError: must be type, not classobj


    错误原因:

    使用super初始化父类只对父类为新风格(new-style)的生效,对旧风格的不生效。

    解决方式1:

    #!/usr/bin/env python
    
    # 使用new-style
    class Bird(object):
            def __init__(self):
                    self.hungry = True
    
            def eat(self):
                    if self.hungry:
                            print 'Aaaah...'
                            self.hungry = False
                    else:
                            print 'No, Thanks!'
    
    
    class SongBird(Bird):
            def __init__(self):
                    super(SongBird, self).__init__()
                    self.sound = 'Squawk!'
    
            def sing(self):
                    print self.sound

    解决方法2:

    #!/usr/bin/env python
    
    __metaclass__ = type
    
    class Bird:
            def __init__(self):
                    self.hungry = True
    
            def eat(self):
                    if self.hungry:
                            print 'Aaaah...'
                            self.hungry = False
                    else:
                            print 'No, Thanks!'
    
    
    class SongBird(Bird):
            def __init__(self):
                    super(SongBird, self).__init__()
                    self.sound = 'Squawk!'
    
            def sing(self):
                    print self.sound

    解决方法3(使用old-style):

    #!/usr/bin/env python
    
    class Bird():
            def __init__(self):
                    self.hungry = True
    
            def eat(self):
                    if self.hungry:
                            print 'Aaaah...'
                            self.hungry = False
                    else:
                            print 'No, Thanks!'
    
    
    class SongBird(Bird):
            def __init__(self):
                    # 不使用super进行初始化
                    Bird.__init__(self)
                    self.sound = 'Squawk!'
    
            def sing(self):
                    print self.sound
  • 相关阅读:
    给树莓派安装中文环境
    windows中让secureCRT正确显示中文(ssh)
    C# net winform wpf 发送post数据和xml到网页
    WebDav协议基于HTTP 1
    WCF WEB HTTP请求 WCF REST FUL
    WPF黑色背景下常用控件样式
    解决WCF大数据量传输 ,System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接
    WPF 漏斗控件 等待沙漏效果
    WPF 资源管理器 WPF Explorer
    WPF MVVM 关闭窗体
  • 原文地址:https://www.cnblogs.com/imoing/p/3787673.html
Copyright © 2011-2022 走看看