I have trouble understanding this strange behavior of Global variable in Python
I would like to explain the code is brief: Here I have a function populate_sheet, that compares the same column in two different dataframes and returns another dataframe having uncommon records.
Next, If the columnName is u Lead-Programme of interest . I want to the processing of the column as per the function modifyPoI_0, that returns the first element of the tuple.
Here is where the problem is, I have defined a variable maxLengthPOI, and made it global, so that it contains the maximum length of the tuple in the column u Lead-Programme of interest
Running this code gives ERROR: global name maxlengthPOI not defined
def populate_sheet(df, input, output, sheet_name, sheet, columnName, sheetInputIndex, sheetOutputIndex ): differentPosition = pd.notnull(output[columnName]) & (input[columnName] != output[columnName]) df = pd.DataFrame(columns = [sheet[sheetInputIndex], sheet[sheetOutputIndex]] ) df[sheet[sheetInputIndex]] = pd.Series(input.loc[differentPosition, columnName]) df[sheet[sheetOutputIndex]] = pd.Series(output.loc[differentPosition, columnName]) if columnName == u Lead-Programme of interest : #Identify the max length of the tuple : maxLengthPOI #THROWS ERROR: Global name maxLengthPOI is not defined. maxLengthPOI = 1 def modifyPoI_0(value): global maxLengthPOI if type(value) == tuple: if maxLengthPOI < len(value): maxLengthPOI = len(value) return value[0] else: return value df[sheet[sheetOutputIndex]] = df[sheet[sheetOutputIndex]].apply(modifyPoI_0) output.loc[differentPosition, columnName] = [ = + sheet_name + ! + xl_rowcol_to_cell(i+1,sheetOutputIndex ) for i in range(sum(differentPosition))] return df
But when I run a simple script
ser = pd.Series([(1, 2), (3, 4, 5, 6), 1, 2]) maxLengthPOI = 1 def modifyPoI_0(value): global maxLengthPOI #basically in LeadPoI if its a tuple then return the if type(value) == tuple: if maxLengthPOI < len(value): maxLengthPOI = len(value) return value[0] else: return value ser.apply(modifyPoI_0)
correctly gives output
0 (1, 2) 1 (3, 4, 5, 6) 2 None 3 None and maxLengthPOI = 4
How come we have this strange behaviour, where in the first case it throws an error and in the second case the same exact code works correctly as expected