3
Follow
0
Followers
我发现一个问题,为什么我return一个字典全局变量,再调用时,变量会被覆盖啊,代码在下面,#号的那三行按理来说打印出来应该也是1,2,3啊,但是实际打印出来是3,3,3搞不明白。这是我回测中发现的问题,发现结果打印出来都一样,然后一步一步找发现根源出在这里。我把全局变量改成局部变量就没这问题,但我想搞明白为什么这样不行,求大佬解答。
def fun(a):
dict_zz['one'] = a
return dict_zz
def main():
params()
dict_zz_15min = fun(1)
dict_zz_1h = fun(2)
dict_zz_4h = fun(3)
print(dict_zz_15min) #
print(dict_zz_1h) #
print(dict_zz_4h) #
def params():
global dict_zz
dict_zz = {}
main()
为什么我打印出来是这个样子
{'one': 3}
{'one': 3}
{'one': 3}
Related Recommendations
How to Specify Different Versions of Data for the Rented Strategy by Its Rental Code MetadataAdvanced Tutorial for FMZ Quant platform Strategy WritingElementary Tutorial for FMZ Quant platform Strategy WritingGet Started with FMZ Quant PlatformSECURITY BUGI keep getting error: Exchange_GetAccount: Invalid ContractTypeWe have an incredibly profitable market making algorithm for sideways markets on Bitmex - but need expert to help eliminate wait times during downward volatility in the marketError with deribitLimitations of the backtesting engineHow to install ta-lib on linux docker?
Comment
All comments (6)
不是key的问题吧,我全局变量换成局部变量就没问题的啊,像这样key不也覆盖的吗?
def fun(a):
dict_zz = {}
dict_zz['one'] = a
return dict_zz
def main():
dict_zz_15min = fun(1)
dict_zz_1h = fun(2)
dict_zz_4h = fun(3)
print(dict_zz_15min)
print(dict_zz_1h)
print(dict_zz_4h)
main()
5 years ago
- 1
