3
Follow
0
Followers
Python のグローバル変数に問題があります。解決方法を教えていただけますか。
Created 2021-09-08 14:20:01 Updated 2021-09-08 15:09:44
6
1198
なぜ辞書の一つのグローバル変数を返すと,呼び出すと変数が覆われるのかという疑問に気づきました. #という3行は,理屈的には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
