pythoncompiler-errorstypeerrorxpress-optimizer

TypeError: 'int' object is not callable, assigning equal names to diff variables?


I've been at this problem for a few hours now, I seem to have named an integer variable and a function the same but I just don't know which variable/function I am assigning the same name to. Here's the function where the error occurs:

def Automatic(fee):
    excel_file = "stocklist.xlsx"
    data = pd.read_excel(excel_file)
    #print(data)
    L = data["Unnamed: 0"].tolist()
    print("Stock list generated:", L)
    p = []
    print("Transaction fee: ",fee*100, "%")
    print('----------------------------------------')
    for i in range(len(L)):
        p.append(float(stock_info.get_live_price(L[i])))
        print("Current price of "+L[i]+": ",p[i])
    print('----------------------------------------')
    return [L,p]

The error that appears on the terminal is:

<ipython-input-35-df30698b28ab> in Automatic(fee)
    156     print("Transaction fee: ",fee*100, "%")
    157     print('----------------------------------------')
--> 158     for i in range(len(L)):
    159         p.append(float(stock_info.get_live_price(L[i])))
    160         print("Current price of "+L[i]+": ",p[i])

TypeError: 'int' object is not callable

Edit: when I do print(type(range)), it returns class so i guess that is the problem, does anyone know where this instance is attributed in the package xpress? I did not assign any int variable the name "range".


Solution

  • The problem is that the xpress package defines constants like leq, geq, eq, and range to specify the type of constraints (<=, >=, ==, or ranged). If you just import everything from the package then this will overwrite the builtin definition of range with an integer constant (in this case 4).

    So it is better to limit the things you pull into your namespace by doing something like

    from xpress import leq, geq, eq, problem
    

    or whatever it is you need. Our just do

    import xpress as xp
    

    and qualify all the xpress-related things by prepending xp.. This has the advantage that it is immediately clear that you are calling xpress-related functions.