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

调用Python类方法时出现TypeError: 'list'对象不可调用的排查求助

TypeError: 'list' object is not callable 问题排查与解决

错误信息

Traceback (most recent call last):
  File "/home/arksdf/Repos/alura/Iesb_DeepLearning/tentativas/teste.py", line 40, in <module>
    model, train_loss, valid_loss = r.classificacao(optimizer, criterion)
  File "/home/arksdf/Repos/alura/Iesb_DeepLearning/tentativas/Runner.py", line 21, in classificacao
    model, train_loss, valid_loss = t.train(self.model, optimizer, criterion)
TypeError: 'list' object is not callable

错误原因

这个错误的核心是:你创建的Treinamento类实例t中,train不是可调用的方法,而是一个列表对象。

出现这种情况的唯一可能是:在Treinamento类的代码中,你错误地将self.train赋值成了列表(或其他非可调用对象),覆盖了原本应该定义的train方法。

排查与修复步骤

  1. 打开Treinamento类的代码,搜索所有self.train的出现位置
  2. 找到类似self.train = []或self.train = [xxx, yyy]的赋值语句,这类语句会把方法名train当成普通变量使用,直接覆盖了类的方法
  3. 将该变量名修改为其他名称(比如self.train_history、self.train_loss_list),避免和方法名冲突
  4. 同时确认Treinamento类中确实存在def train(self, model, optimizer, criterion):的方法定义,且方法名拼写正确(大小写、单词完全匹配)

错误示例(Treinamento类中)

class Treinamento:
    def __init__(self, dataset, epochs, batch_size, early_stopping_epochs):
        self.dataset = dataset
        self.epochs = epochs
        self.batch_size = batch_size
        self.early_stopping_epochs = early_stopping_epochs
        self.train = []  # 此处错误覆盖了train方法

修正示例

class Treinamento:
    def __init__(self, dataset, epochs, batch_size, early_stopping_epochs):
        self.dataset = dataset
        self.epochs = epochs
        self.batch_size = batch_size
        self.early_stopping_epochs = early_stopping_epochs
        self.train_history = []  # 修改变量名,避免与方法冲突

    def train(self, model, optimizer, criterion):
        # 你的训练逻辑代码
        # ...
        return model, train_loss, valid_loss

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 19:40:27