You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中为何报错:'str' object has no attribute 'head'?

错误原因及修复方案

核心问题:实例方法调用方式错误

你直接通过类名interface(别名tp)调用了comp方法,但comp是类的实例方法(定义时第一个参数为self),这类方法必须通过类的实例对象调用,Python才会自动把实例作为self参数传入。

你的调用代码:

terminal = tp.comp(a, b, c, d, e, f, fakedirs, g, g)

这里直接用类tp调用comp,导致第一个参数a(值为"_",字符串类型)被当成self传入方法。执行self.head = head时,self是字符串对象,而字符串没有head属性,因此抛出AttributeError: 'str' object has no attribute 'head'。

修复步骤

  1. 先创建interface类的实例对象
  2. 通过实例对象调用comp方法

修正后的代码:

from tub_rpg import interface as tp

fakedirs = {"Media": ["Picture.jpg"], "Documents":["Profits.xls"], "Backup":["Profits-Copy.xls"], "Games":["DOOM.wad"]}

# 去掉末尾多余的逗号
a,b,c,d,e,f,g = "_", "[", "]", "[", "]", "ADMIN", 20

# 实例化类
tp_instance = tp()
# 通过实例调用comp方法
terminal = tp_instance.comp(a, b, c, d, e, f, fakedirs, g, g)

内容的提问来源于stack exchange,提问作者user15298418

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:48:17