0

I have 2 Python files and in the first one I am calling a function from the second one. However, I always get the error "ValueError: too many values to unpack (expected 2)" and I don't know why I get it. The function in the second files takes 16 arguments and returns 16 arguments. These 16 returned arguments are assigned to 16 variables in the first file. Still I get this error everytime. Does anyone of you have a clue what the problem might be?

Here you can see the call from the funtion call from the first file:

    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)

Here you see the function "simulateTimeSlot_WithAddtionalControler_BT1 "from the second file "ICSimulation":

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 

What is really confusing to me is why it says that "2" are expected? The function takes 16 arguments and returns 16 arguments. I do not know at all why only 2 are expected?

Eladtopaz
  • 1,036
  • 1
  • 6
  • 21
PeterBe
  • 700
  • 1
  • 17
  • 37
  • 3
    it's really hard to understand that way. which line does the error refers to? in your first block code, where is the call? third - non related to your issue but if you have so many arguments to a function, it highly recommended you to create an object that holds these arguments (or at least some objects) and pass them instead – tomer Sep 15 '21 at 12:59
  • 1
    You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line – data_m Sep 15 '21 at 13:00
  • 1
    As per @data_m's comment, from the interpreter's point of view you have a series of lines that simply state some variable names (and thus do nothing) and then `self.helpCounterNumberOfStandBySlots_Combined, self.helpCurrentPeakLoad = ICSimulation.simulateTimeSlot_WithAddtionalControler_BT1 ([...])`, which is unpacking the returned values into just those two variables. – Kemp Sep 15 '21 at 13:03
  • Thanks for your answers all. Basically I could solve the problem when putting everything in one line. It looks quite weird as the line is extremely long (I use different screens because of this the backslash does not make too much sense). – PeterBe Sep 15 '21 at 14:12
  • @tomer: What is the advantage of creating a separate object that holds all the data? Basically for that I would have to define another separate file and import it everywhere which makes the project (slightly) more complex – PeterBe Sep 15 '21 at 14:14
  • 1
    @PeterBe it's just a bit more readable and orgenized. it doesn't really matter for a small script but if it's something you're going to maintain and use for a while it's a better practive. you can check this refernce to Clean Code: https://matheus.ro/2018/01/29/clean-code-avoid-many-arguments-functions/ there a few good pratices how to write a cleaner code more than just that. – tomer Sep 15 '21 at 14:42
  • @tomer: Thanks for your comment and the link. In my case I have "self." variables which belong to the class. So when I create a new object I would have to first assign the values of the "self."-variables to the object and then after the calculation I would have to assign back the values of the object to all the 16 "self."-variables. So I am not sure whether this is really better regarding organization and structure of the code. – PeterBe Sep 15 '21 at 15:19
  • 1
    @PeterBe I still think it's should be more organized (my opinion only). you can create objects and hold them in your "self". notice that you already kind of know the objects appeared in your arguments - that was your hint to use "state_*", "action_* etc. I think that indicate you should hold object in self called State and Action that are holding usableVolumeDHWTank, SOCOfTheEV etc. I think after a refacot you'll notice how clearer it is. but again, this is only my opinion. good luck :) – tomer Sep 15 '21 at 16:41
  • @tomer: Thanks for your answer tomer. How can I define a self object within the same class? And how can I then use it with the self operator? I defined an inner class with 2 variables for testing `class State(): def __init__(self, state_outsideTemperature,state_PVGeneration ): self.state_outsideTemperature = state_outsideTemperature self.state_PVGeneration = state_PVGeneration` But when I try to create an object in a method of the upper class by using `state = State( 1,2)` I get the NameError: name 'State' is not defined – PeterBe Sep 16 '21 at 12:14
  • @tomer: Any comments to my last comment? I'd highly appreciate every further comment from you. – PeterBe Sep 17 '21 at 13:49
  • @tomer: Any comments to my last comment? I'd highly appreciate every further comment from you. – PeterBe Sep 20 '21 at 15:30
  • @tomer: Any comments to my last comment? I'd highly appreciate every further comment from you – PeterBe Sep 23 '21 at 08:29
  • 1
    @PeterBe sorry I'mreally busy lately. it's really hard to undersatnd the issue from writing it in the comment, I suggest you edit your question with a more orgnized code block and I'll try to help. I think the problem is the location of the class State you've defined. it should be in the same level as your first class and not inside – tomer Sep 27 '21 at 08:32

0 Answers0