-1

I got a dict that needs to be populated with values returned from a method. The method uses two lists defined before the method, to search if the passed argument exists inside the lists. However, the lists can't be accessed from within the method? "Undefined variable" it tells me for both of them. The two lists that I am referring to are dbBUYlist and nbBUYlist.

Some code below:

nbBUYlist = []
dbBUYlist = []

#code to populate both lists from webscraping

def tickervalue(self, ticker):
    recCount = 0

    if ticker in dbBUYlist:
        recCount + 1

    if ticker in nbBUYlist:
        recCount + 1

    return recCount

recdict = {ticker : tickervalue(ticker) for ticker in tickers}
buyrec = pd.DataFrame.from_dict(recdict, orient = 'index', columns=['Sum']).sort_values('Sum', ascending=False)[:10]

buyrec
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bullfighter
  • 397
  • 1
  • 4
  • 21

2 Answers2

1

You should be sure that you can access them just before that function is defined.

nbBUYlist = []
dbBUYlist = []

#code to populate both lists from webscraping

print (nbBUYlist) # <--------------- added
def tickervalue(self, ticker):
    # ...

If that print is throwing the same error, it means python cannot locate the lists within that scope. If they are in a different .py file, you'll have to import them. e.g:

import module_with_lists;
print(module_with_lists.nbBUYlist)

Python keeps track of objects loaded into each "python file", known as the module, and needs to be told which items to use where.

If the variables are defined in the same module, you can try a functools.partial to make sure the lists are passed to the scope of the function.

import functools

#                     |              | <- defined in partial
def tickervalue(self, nb_list, db_list, ticker):
    return (ticker in nb_list) + (ticker in db_list)

# Creates a function that has the first two arguments always supplied
ticker_function = functools.partial(tickervalue, nbBUYlist, dbBUYlist)

# Use the function returned from that partial
recdict = {ticker : ticker_function(ticker) for ticker in tickers}

Edit: With self in the function call, it's worth pointing out that if the tickervalue function is part of a class, than obviously, the print() just before the tickervalue won't work. I recommend instead that you post a possibly more complete example of your code as it's challenging to reconcile without more context.

And as someone pointed out in the comments, if you have defined nbBUYlist and dbBUYlist in a class, you need to access them as such:

class Foo():
    nbBUYlist = []
    dbBUYlist = []

    def tickervalue(self, ticker):
        # Get with self
        return (ticker in self.nbBUYlist) + (ticker in dbBUYlist)
mccatnm
  • 212
  • 1
  • 12
  • Thanks! self. solved it. I suppose doing something like Foo.dbBUYlist (accessing the list as a property of the class itself) could work the same? – bullfighter Aug 16 '19 at 15:13
  • 1
    That's correct! `Foo.dbBUYlist` would work as well. A word of advice: If you subclass `Foo` and augment the values of the subclass, `Foo.dbBUYlist` would still be the original values. – mccatnm Aug 16 '19 at 15:25
0

There are multiple things wrong with this code. I'm not sure why you can't access the variables, the following code works on my machine.

nbBUYlist = []
dbBUYlist = []

#code to populate both lists from webscraping
def tickervalue(ticker): # remove self
    recCount = 0

    if ticker in dbBUYlist:
        recCount += 1 # missing equals sign

    if ticker in nbBUYlist:
        recCount += 1 # missing equals sign

    return recCount

recdict = {ticker : tickervalue(ticker) for ticker in tickers} # tickers is never defined, not sure what you mean here

buyrec = pd.DataFrame.from_dict(recdict, orient = 'index', columns=['Sum']).sort_values('Sum', ascending=False)[:10]

buyrec
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
  • damn, that's unusual coming back from Java again. Anyways, I added global to the two lists, but now it says the syntax is invalid? I got the code inside a class by the way, so perhaps there is some initialization step I have missed? – bullfighter Aug 16 '19 at 13:27
  • Thought the same, but then how come they show as undefined variables? :o – bullfighter Aug 16 '19 at 13:30
  • I ran the code on my machine and updated it accordingly @bullfighter – Josh Correia Aug 16 '19 at 13:42