The resource loading... loading...

Python global variables encountered a problem and asked God for an answer.

Author: tony233, Created: 2021-09-08 14:20:01, Updated: 2021-09-08 15:09:44

I found a problem, why I return a dictionary global variable, and when I call it again, the variable is overwritten, the code is below, and the three lines of code # are theoretically supposed to be printed as 1, 2, 3, but actually printed as 3, 3, 3. I don't understand. def fun ((a): dict_zz [supplemented with salt] = a return dict_zz is used def main (: Params ((() I'm not sure what you mean. dict_zz_1h = fun ((2) dict_zz_4h = fun ((3) I'm going to go to the bathroom. I'm going to print it. I'm not sure.

def params(): global dict_zz dict_zz = {} main()

Why did I print it like this? What's that? What's that? What's that?


More

The grassYou're learning the basics of Python.

tony233You just say that the dictionary is shorthand and I get it... my mom, who has been learning Python for so long, doesn't understand this.

The grass def fun(a): dict_zz['one'] = a return dict_zz def main(): params() dict_zz_15min = fun(1) print(dict_zz_15min) dict_zz_1h = fun(2) print(dict_zz_1h) dict_zz_4h = fun(3) print(dict_zz_4h) # def params(): global dict_zz dict_zz = {} main()

tony233You're a bit of a jerk, what's a python base?

tony233It's not a key problem, I'm just switching global variables to local variables, so it's okay, like, key doesn't even cover that? def fun ((a): dict_zz = {} dict_zz ['one'] = a return dict_zz is used def main (: I'm going to have fun. dict_zz_1h = fun ((2) dict_zz_4h = fun ((3) I'm going to try to get a copy of this. print ((dict_zz_1h)) print ((dict_zz_4h)) main ((()

tony233It's not a key problem, I'm just switching my global variables to local ones.