super() fails with error: TypeError “argument 1 must be type, not classobj”

本文探讨Python面向对象编程中new-style类的重要性。通过一个具体示例,讲解了如何解决super()函数调用时出现TypeError的问题,并给出了两种有效的解决方案。

关于Python的面向对象的编程有这样一个例子:

class Bird:
    def __init__(self):
        self.hungry = 1
    def eat(self):
        if self.hungry:
            print 'Aaaah...'
            self.hungry = 0
        else:
            print 'No, thanks!'

class SongBird(Bird):
    def __init__(self):
#        super(SongBird,self).__init__()
        self.sound = 'Squawk!'
    def sing(self):
        print self.sound

def test():
    t=SongBird()
    t.sing()
    t.eat()

if __name__ == '__main__':
    test()
这时运行会报错,按照C++的OO特性来了解,SongBird类继承了Bird类的方法,包括魔术方法构造函数,在new出一个Songird类的对象t时,会自动执行父类的构造函数,但是在python中并不是这样,而是的借助super()这个函数来显式调用才会生效,如果没有super那个函数,会报错

加上后,同样会报错,报错信息为:

super() fails with error: TypeError “argument 1 must be type, not classobj”

参考stackoverflow的解决说明:

Your problem is that class B is not declared as a "new-style" class. Change it like so:

class B(object):

and it will work.

super() and all subclass/superclass stuff only works with new-style classes. I recommend you get in the habit of always typing that (object) on any class definition to make sure it is a new-style class.

Old-style classes (also known as "classic" classes) are always of type classobj; new-style classes are of type type. This is why you got the error message you saw:

TypeError: super() argument 1 must be type, not classobj

所以解决方法有两种:

1.在开头显示说明使用new-style classes,在开头加上__metaclass__=type

2.sof上的解决方法

这两个错误通常在使用 Git 时出现,以下是一些可能的解决方法: ### 1. 检查 Git 版本 确保使用的是最新版本的 Git,旧版本可能存在一些已知的 bug。可以使用以下命令更新 Git: ```bash # 对于 Ubuntu/Debian sudo apt-get update sudo apt-get install git # 对于 CentOS/RHEL sudo yum install git # 对于 macOS brew update brew upgrade git ``` ### 2. 检查仓库完整性 使用 `git fsck` 命令检查仓库的完整性。该命令会检查仓库中的对象是否有损坏或丢失: ```bash git fsck --full ``` ### 3. 清理暂存区 有时候暂存区的文件可能会导致问题,可以尝试清理暂存区: ```bash git reset --hard HEAD ``` ### 4. 重建索引 如果问题仍然存在,可以尝试重建索引: ```bash rm -f .git/index git reset ``` ### 5. 从远程仓库拉取 如果本地仓库损坏,可以尝试从远程仓库重新克隆: ```bash git clone <remote-repository-url> ``` ### 6. 修复对象 如果是某个对象损坏,可以尝试从其他副本中恢复该对象。 ### 7. 检查文件权限 确保仓库目录及其文件的权限设置正确,避免因权限问题导致无法访问或修改对象: ```bash chmod -R 755 .git ``` ### 8. 检查环境变量 确保 `GIT_OBJECT_DIRECTORY` 和 `GIT_ALTERNATE_OBJECT_DIRECTORIES` 等环境变量没有被错误设置。 ### 9. 检查磁盘空间 确保磁盘有足够的空间来存储 Git 对象。 ### 10. 检查文件系统 确保文件系统没有损坏,可以使用磁盘检查工具进行检查和修复。 ### 代码示例 如果要编写一个简单的脚本来自动化上述部分步骤,可以使用以下 Python 脚本: ```python import subprocess def run_command(command): try: result = subprocess.run(command, shell=True, check=True, text=True, capture_output=True) print(result.stdout) except subprocess.CalledProcessError as e: print(f"Error: {e.stderr}") # 检查仓库完整性 run_command("git fsck --full") # 清理暂存区 run_command("git reset --hard HEAD") # 重建索引 run_command("rm -f .git/index") run_command("git reset") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值