Is input arguments to a function returned always by default?

Tags: , , , ,



I have a function like given below. The code works fine and I require your assistance only in helping me to understand how the return or function output is stored. I am a beginner and your inputs would be helpful

dataFramesDict = dict()

def create_df(xls,s): 
   ......
   return sheet_df,sheet_name

def transform_stage_1_df(sheet_df,sheet_name):
    ..... 
    .....
    return sheet_df,sheet_name

def transform_stage_2_df(sheet_df, sheet_name):

     result = pd.concat(....)
     return result

As you can see, I am only returning the result dataframe as output from the function. No other variables are returned from function

When I call the function in sequence like below, I expect it throw an error at the last line for dataFramesDict[sheet_name] but it works fine.

sheet_df,sheet_name = create_df(xls,s)
sheet_df,sheet_name = transform_stage_1_df(sheet_df,sheet_name)
dataFramesDict[sheet_name] = transform_stage_2_df(sheet_df,sheet_name)

Shouldn’t I be getting an error message like below

sheet_name isn’t defined

I tried restarting the kernel to make sure that it’s not defined elsewhere.

Because sheet_name isn’t a global variable. It is only passed as an argument to the above function. Does function always return input arguments by default as well?

Answer

Variable sheet_name is defined outside transform_stage_2_df as you are using it as an input parameter and thus is already defined outside.

dataFramesDict[sheet_name] = transform_stage_2_df(sheet_df,sheet_name)


Source: stackoverflow