Home > Enterprise >  why i cannot pass a tuple to a function (which adress is reference in a dict), i have a error messag
why i cannot pass a tuple to a function (which adress is reference in a dict), i have a error messag

Time:01-19

i have the below code : (df_data is a dataframe in this code)

def instr_date(df_data,num):
    return df_data.index[-num]

dict_func = {'instr_date' : instr_date,}
dict_arg = {'instr_date' : (df_data_instr,),}
dict_col_func = {'instr_date' : (1,),}

dict_data = {}
for k,v in dict_col_func.items() :
    dict_data[k]=dict_func[k](dict_arg[k] v)

This doesn't work, i have the error message :

TypeError: instr_date() missing 1 required positional argument: 'num'

I tried:

  • dict_funt[k](df_data,1) to check if i have an issue with my tuple (so i pass manually a tuple to the dict_func[k]) and it works!
  • when i do (dict_arg[k] v) == (df_data,1) the result is "True".

So why i don't have the same behavior with 2 identical tuple ?
Anyone has an idea to help ?

CodePudding user response:

Your function wants two arguments, not a tuple containing the two arguments. But you can expand the tuple in the call. Change:

dict_func[k](dict_arg[k] v)

to:

dict_func[k](*(dict_arg[k] v))

This will expand the tuple when forming the argument list in the call.

  • Related