[英]global variable between functions is always = None python
I have this code:我有这个代码:
last_price=None
def trade(price,last_price=last_price)
if last_price == None:
print("last_price=None!")
last_price = price
#do something else
while True:
price= get_price_function()
trade(price)
Buy I have the problem - last_price is always None(it seems that it's outside the scope of function, so how to use global variable last_price , so it is not always none.买我有问题 - last_price 总是 None (似乎它在 function 的 scope 之外,所以如何使用全局变量 last_price ,所以它并不总是没有。
In order to access a global variable inside a function, declare it with global
keyword.为了访问 function 中的全局变量,请使用
global
关键字声明它。
Change your trade
function to:将您的
trade
function 更改为:
def trade(price)
global last_price
if last_price == None:
print("last_price=None!")
last_price = price
#do something else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.