Global name maxLengthPOI not defined in Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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

Answers

When you call

#THROWS ERROR: Global name maxLengthPOI is not defined.
        def modifyPoI_0(value):
            global maxLengthPOI

maxLengthPOI does not exists.

As you see in the first code you posted, maxLengthPOI is not global at the OUTER def. So when you call modifyPoI_0 it does not exists, as it cant get out of populate_sheet. Make it global there as well, and it wont be a problem.

The global stuff can be considered as: If you DON T define global and you change a variable inside a function, the value will NOT change globally, it will ONLY change inside the function.

Consider this:

x = 2
print(x)
def func():
    x = 4
    print(x)
func()
print(x)

print( ---- )

x = 2
print(x)
def func2():
    global x
    x = 4
    print(x)
func2()
print(x)

This prints:

2
4
2
# See? x haven t changed, even i called a function which changes its value (It actually changed inside the function as you can see, but not globally)
----
2
4
4
# Now x changed, because i made the variable global, so it  got out  of the function after its change

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/38395490/global-name-maxlengthpoi-not-defined-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils