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

Python调用多返回值函数报too many values to unpack(expected 2)错误排查

错误根因

该报错由Python的语句换行规则导致:Python默认以换行作为单个语句的结束标志,你的代码中两处多行元组的写法不符合语法要求,导致执行逻辑和预期不符:

  1. 赋值接收端逻辑错误:16个接收变量拆分为5行书写,既没有用圆括号包裹所有变量,也没有在每行末尾添加续行符\。Python会将前4行识别为无赋值动作的独立元组表达式(直接丢弃执行结果),仅将最后一行的2个变量作为赋值接收端,因此报错提示预期仅2个值。
  2. 函数返回端逻辑错误:return后的16个返回值同样拆分为多行书写,未添加括号或续行符。Python仅会将return所在第一行的4个值作为函数返回结果,后续行的内容会被识别为函数外的独立代码,不会被返回。

修复方案

推荐将所有多行元组用圆括号包裹,Python会自动识别括号内的换行为续行,无需添加额外符号:

1. 调用侧赋值代码修改

(
    self.action_heatCoefficientSpaceHeating, self.action_heatCoefficientDHW, self.action_chargingPowerEV, self.state_temperatureBufferStorage,
    self.state_usableVolumeDHWTank, self.state_SOCOfTheEV, self.help_countNumberOfStartHeatPump_HeatingBufferStorage_Indiviual, self.help_countNumberOfStartHeatPump_HeatingDHW_Indiviual,
    self.help_countNumberOfStartHeatPump_Heating_Combined , self.helpCounterNumberOfRunningSlots_SpaceHeating , self.helpCounterNumberOfRunningSlots_DHW ,
    self.helpCounterNumberOfRunningSlots_Combined , self.helpCounterNumberOfStandBySlots_SpaceHeating , self.helpCounterNumberOfStandBySlots_DHW ,
    self.helpCounterNumberOfStandBySlots_Combined, self.helpCurrentPeakLoad
) = ICSimulation.simulateTimeSlot_WithAddtionalControler_BT1(
    self.action_heatCoefficientSpaceHeating, self.action_heatCoefficientDHW, self.action_chargingPowerEV, self.help_statePreviousTimeSlot_temperatureBufferStorage,
    self.help_statePreviousTimeSlot_usableVolumeDHWTank, self.help_statePreviousTimeSlot_SOCOfTheEV, self.help_countNumberOfStartHeatPump_HeatingBufferStorage_Indiviual,
    self.help_countNumberOfStartHeatPump_HeatingDHW_Indiviual, self.help_countNumberOfStartHeatPump_Heating_Combined , self.helpCounterNumberOfRunningSlots_SpaceHeating , self.helpCounterNumberOfRunningSlots_DHW ,
    self.helpCounterNumberOfRunningSlots_Combined , self.helpCounterNumberOfStandBySlots_SpaceHeating , self.helpCounterNumberOfStandBySlots_DHW , self.helpCounterNumberOfStandBySlots_Combined, self.helpCurrentPeakLoad
)

2. 函数返回代码修改

def simulateTimeSlot_WithAddtionalControler_BT1 (action_SpaceHeating, action_DHWHeating, action_EVCharging, state_BufferStorageTemperatureLastTimeSlot,
                                                 state_usableVolumeDHWLastTimeSlot, state_SOCofEVLastTimeSlot, helpCountNumberOfStartsIndividual_SpaceHeating, 
                                                 helpCountNumberOfStartsIndividual_DHW, helpCountNumberOfStarts_Combined , helpCounterNumberOfRunningSlots_SpaceHeating , helpCounterNumberOfRunningSlots_DHW ,
                                                 helpCounterNumberOfRunningSlots_Combined , helpCounterNumberOfStandBySlots_SpaceHeating , helpCounterNumberOfStandBySlots_DHW ,
                                                 helpCounterNumberOfStandBySlots_Combined, helpCurrentPeakLoad ):

    
    state_BufferStorageTemperatureCurrent =-1
    state_usableVolumeDHWCurrent = -1
    state_SOCofEVCurrent = -1
    action_SpaceHeating_corrected = -1
    action_DHWHeating_corrected = -1
    action_EVCharging_corrected = -1
    
    # 用括号包裹所有返回值
    return (
        action_SpaceHeating_corrected, action_DHWHeating_corrected, action_EVCharging_corrected, state_BufferStorageTemperatureCurrent,
        state_usableVolumeDHWCurrent, state_SOCofEVCurrent, helpCountNumberOfStartsIndividual_SpaceHeating, helpCountNumberOfStartsIndividual_DHW,
        helpCountNumberOfStarts_Combined , helpCounterNumberOfRunningSlots_SpaceHeating , helpCounterNumberOfRunningSlots_DHW ,
        helpCounterNumberOfRunningSlots_Combined , helpCounterNumberOfStandBySlots_SpaceHeating , helpCounterNumberOfStandBySlots_DHW ,
        helpCounterNumberOfStandBySlots_Combined, helpCurrentPeakLoad
    )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 18:48:00