我编写了一些多次调用函数的小型 Python 脚本。该函数应始终返回其他值,因为缓冲区始终是唯一的(由于.pop(0)
)。但回报总是一样的。如果我在循环之外调用该函数两次result1 = x(buffer1)
,结果是唯一的。result2 = x(buffer2)
但是一旦我尝试像在伪代码中那样调用函数,结果总是一样的。似乎 Python 只执行一次函数,然后打印它的结果。我已经在PHP
and中实现了这段代码VB.net
,它可以正常工作。这似乎是一个特定的 Python 事物。有人可以解释一下。:/
伪代码
function x()
list = []
buffer = []
for l in list:
buffer.append(l)
if len(buffer) == y:
return = x(buffer)
buffer.pop(0)
Python-代码
ema_length = {66:0,74:0,89:0}
def ema(length):
relevant = len(length)
multiplier = 2 / (relevant + 1)
sum_vector = 0
for vector in length:
sum_vector += vector
current_vector = vector
sma = sum_vector / relevant
if ema_length [relevant] == 0:
ema_length [relevant] = sma
else:
ema_length [relevant] = (current_vector - ema_length [relevant]) * multiplier + ema_length [relevant]
return ema_length [relevant]
buffer = []
for d in data:
buffer.append(d)
if len(buffer) == 89:
ema66 = ema(buffer [89 - 66:89]) # should be unique
ema74 = ema(buffer [89 - 74:89]) # should be unique
ema89 = ema(buffer [89 - 89:89]) # should be unique
ema_overall = ema66 - ema74 - ema89
buffer.pop(0)