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)