【发布时间】:2014-09-24 04:13:46
【问题描述】:
我有 2 个函数包含在 2 个单独的文件中。
这些函数是相似的,并且它们中的局部变量使用相同的名称。传递给每个函数的参数“eachtickerlist”也是相同的。 eachtickerlist 是一个看起来像这样的列表的列表
[[ticker,price,year,month,day,seconds from epoch],[ticker,price,year.........
由于某种原因,每个函数“amlist”中的局部变量在执行函数 2 时会保留来自函数 1 的数据,即使它在第二个函数中重新初始化并且即使它是一个局部变量。我知道这一点是因为当我在函数 2 的末尾打印出“amlist”以对其进行测试时,字符串“未定义”、“向上”或“向下”被列出了 2 次。如果我不在主程序中调用函数 1,则不会发生这种情况,这证明函数 1 中发生的事情正在影响函数 2……我完全不明白。
在我的主程序中,我调用每个函数,如下所示:
eachtickerlist=sorted(eachtickerlist,key=operator.itemgetter(6)) #This is the argument to be passed
upsvsdownsresult=upsvsdowns.upsvsdowns(eachtickerlist) #This sends the argument to the first function and stores the return value for use later
swingsresult=swings.swings(eachtickerlist) #This sends the same argument to the second function and stores the return value for use later
这里是功能1:
def upsvsdowns(eachtickerlist):
amlist=[]
for thing in eachtickerlist:
if (thing[5]=='8'):
amlist.append(thing)
else:
pass
try:
amlist[0].append('undefined')
length=len(amlist)
for x in range(1,length):
price=float(amlist[x][1])
yesterdaysprice=float(amlist[(x-1)][1])
if ((price-yesterdaysprice)>0):
amlist[x].append('up')
else:
amlist[x].append('down')
upcount=0
totalcount=0
for y in amlist:
if (y[7]=='up'):
upcount=upcount+1
totalcount=totalcount+1
else:
totalcount=totalcount+1
percentage=round(float(upcount)/float(totalcount),3)
returnlist=[amlist[0][0],str(percentage)]
return (returnlist)
except (IndexError):
returnlist=[eachtickerlist[0][0],'No Data']
return (return list)
这里是功能2:
def swings(eachtickerlist):
amlist=[]
for thing in eachtickerlist:
if (thing[5]=='8'):
amlist.append(thing)
else:
pass
try:
amlist[0].append('undefined')
length=len(amlist)
for x in range(1,length):
price=float(amlist[x][1])
yesterdaysprice=float(amlist[(x-1)][1])
if ((price-yesterdaysprice)>0):
amlist[x].append('up')
else:
amlist[x].append('down')
upcount=0
downcount=0
ups=[]
downs=[]
print amlist
for y in amlist:
if (y[7]=='up'):
if (downcount!=0):
downs.append(downcount)
else:
pass
downcount=0
upcount=upcount+1
elif (y[7]=='down'):
if (upcount!=0):
ups.append(upcount)
else:
pass
upcount=0
downcount=downcount+1
if (upcount!=0):
ups.append(upcount)
elif (downcount!=0):
downs.append(downcount)
else:
pass
#print ups
#print downs
try:
averageup=round(sum(ups)/float(len(ups)),3)
except(ZeroDivisionError):
averageup=round(0.0,3)
try:
averagedown=round(sum(downs)/float(len(downs)),3)
except(ZeroDivisionError):
averagedown=round(0.0,3)
returnlist=[amlist[0][0],str(averageup),str(averagedown)]
return (returnlist)
except (IndexError):
returnlist=[eachtickerlist[0][0],'No Data']
return (return list)
这是第二个函数中 print 语句的输出。请注意每个列表中的 2 个未定义的 up 和 down。
['AAIT', '35.09', '2014', '7', '28', '8', '2409480.0', 'undefined', 'undefined'], ['AAIT', '35.21', '2014', '7', '29', '8', '2494662.0', 'up', 'up'], ['AAIT', '40', '2014', '7', '29', '8', '2494662.5', 'up', 'up'], ['AAIT', '42.3', '2014', '7', '29', '8', '2494663.0', 'up', 'up']]
任何帮助将不胜感激。
-布兰登
【问题讨论】:
-
你说你用
upsvsdowns.upsvsdowns调用方法,但声明说它们是全局函数。这些方法是类还是其他数据结构的成员? -
欢迎来到 Stack Overflow!很高兴您包含失败的实际代码(而不是一些伪代码或不准确的描述。)有两件事可以使您的问题更好(并且更有可能引出高质量的答案)。首先,将您的程序缩减为仍然显示错误的最小实际程序。将该简短而完整的程序复制粘贴到您的问题中。其次,要特别注意你的缩进。将您的程序粘贴到 SO 中似乎破坏了它的缩进。再次欢迎!
-
在主程序中,我导入 upsvsdowns.py 和 swings.py 这两个文件,它们只包含各自的功能。我不确定这是否能回答您的问题?
-
下次我会少上传一些代码。对缩进感到抱歉。
标签: python variables python-2.7 global local