I am a rookie. I used the exec method when reading a crawler textbook, but there was one thing I didn"t understand, so I simplified that paragraph into a function.
in the first case, the name variable is connected with both ends with +, the second is written directly in a string, and the third is printed directly
does not understand the difference between the first two, isn"t it all a string? By the way, I"d like to ask you about the scope of exec. My heart is so tired
ask the kind-hearted person, thank you very much
def func ():
list = ["a", "b", "c"]
x = 1
for a in list:
name = "number" + str(x) -sharp
exec(name + "=a") -sharp
exec("print(" + name + ")")
x += 1
func ()
< H1 > a < / H1 > < H1 > b < / H1 > < H1 > c < / H1 >def func ():
list = ["a", "b", "c"]
x = 1
for a in list:
name = "number" + str(x) -sharp
exec(name + "=a") -sharp
exec("print(name)")
x += 1
func ()
< H1 > number1 < / H1 > < H1 > number2 < / H1 > < H1 > number3 < / H1 >def func ():
list = ["a", "b", "c"]
x = 1
for a in list:
name = "number" + str(x) -sharp
exec(name + "=a") -sharp
print(name)
x += 1
func ()
< H1 > number1 < / H1 > < H1 > number2 < / H1 > < H1 > number3 < / H1 >