编辑:为什么人们不赞成这篇文章?Python 开发者真的这么无能吗?这是一个合理的问题,而不是在其他地方得到回答的问题。我寻找解决方案。我不是白痴。一个参数有一个值,另一个是未定义的,但如果你真的阅读了这篇文章,你会发现它们的范围似乎是相同的。
首先,我向您保证,这个问题与其他涉及错误消息的问题不同:
UnboundLocalError: local variable referenced before assignment closure method
当我查看这段代码时,似乎uuidString
顶级方法的参数 ,getStockDataSaverFactory
当方法返回其内部方法时,实际上应该在范围内saveData
,作为一等函数对象......因为令我惊讶的是,参数tickerName
在范围内并且在调用方法时确实具有值“GOOG” saveData()
(例如通过测试方法testDataProcessing_getSaverMethodFactory
),因此我们实际上可以看到在调用方法时它具有实际值getDataMethodFactory(..)
,不像uuidString
.
为了使问题更明显,我添加了以下几行:
localUuidString = uuidString
和
experimentUuidString = localUuidString
当断点检查方法时,表明参数uuidString
具有可用值。
def getStockDataSaverFactory(self, tickerName, uuidString, methodToGetData, columnList):
# This method expects that methodToGetData returns a pandas dataframe, such as the method returned by: self.getDataFactory(..)
localUuidString = uuidString
def saveData():
(data, meta_data) = methodToGetData()
experimentUuidString = localUuidString
methodToNameFile = self.getDataMethodFactory(tickerName, uuidString)
(full_filepathname, full_filename, uuidString) = methodToNameFile()
methodToSaveData = self.getDataFrameSaverFactory(methodToGetData, columnList, full_filepathname)
# We might want try/catch here:
methodToSaveData()
# A parameterless method that has immutable state (from a closure) is often easier to design around than one that expects parameters when we want to pass it with a list of similar methods
return (full_filepathname, full_filename, uuidString)
return saveData
def testDataProcessing_getSaverMethodFactory(self):
dataProcessing = DataProcessing()
getSymbols = dataProcessing.getSymbolFactory(
dataProcessing.getNasdaqSymbols(dataProcessing.getListOfNASDAQStockTickers))
tickers = getSymbols()
uuidString = 'FAKEUUID'
columnList = ['low', 'high']
tickerSubset = tickers[0:2]
methodsToPullData = map(lambda ticker: dataProcessing.getStockDataSaverFactory(ticker,
uuidString,
dataProcessing.getDataFactory(
ticker),
columnList), tickerSubset)
savedPathTuples = [f() for f in methodsToPullData]
savedFileNames = [pathTuple[0] for pathTuple in savedPathTuples]
for fileName in savedFileNames:
self.assertTrue(os.path.isfile(fileName))
os.remove(fileName)
只是为了清楚说明uuidString
没有价值但ticker
确实有价值,我将这个截图包括在内:
请注意,在变量监视窗口中,uuidString
未定义,但ticker
具有字符串值“A”。
Python(或 Python 3)是否有一些独特的东西导致了这种行为?