exec函数没有执行并且没有引发错误 (Python)。

huangapple go评论75阅读模式
英文:

exec function is not executing and is not raising error (python)

问题

我是一名初学者程序员,我试图理解这三个函数之间的区别。原来我的问题有点不同,但我简化了它,不想占用你很多时间。今天我实际上学会了exec函数,无论我如何尝试使用该函数的不同变体,我都无法理解这两个版本v1和v2之间的区别。我想使用v1版本,但它不会更改变量p5。如果有人能帮我澄清,我会很高兴。

注意:原始问题有p1,p2,p3,...,p9,并且我从用户输入中获取索引。因此,格式化字符串很重要。

p5 = 5
index_of_p = 5

def change_p5_v1():
    exec(f"global p{index_of_p}")
    exec(f"p{index_of_p} = 3")
    print(p5)  # 输出 5

def change_p5_v2():
    exec("global p5")
    exec("p5 = 3")
    print(p5)  # 输出 5

def change_p5_v3():
    global p5
    p5 = 3
    print(p5)  # 输出 3

change_p5_v1()
change_p5_v2()
change_p5_v3()
英文:

I'm a beginner programmer and I am trying to understand the difference between these three functions. Originally my problem is a bit different but I made it this easy for not to take your time a lot. I actually learned exec function today and no matter how much I try different variations with the function, I can't understand the difference between these v1 and v2. I want to use the v1 version but it doesn't change the variable p5. If anyone can clarify me I would be so glad.

NOTE: Original problem have p1,p2,p3,...,p9 and I get the index from the user input. So it is important to formatting string.

p5 = 5
index_of_p = 5


def change_p5_v1():
    exec(f"global p{index_of_p}")
    exec(f"p{index_of_p} = 3")
    print(p5)  # Output 5


def change_p5_v2():
    exec("global p5")
    exec("p5 = 3")
    print(p5)  # Output 5


def change_p5_v3():
    global p5
    p5 = 3
    print(p5)  # Output 3


change_p5_v1()
change_p5_v2()
change_p5_v3()

答案1

得分: 2

你可以在你的 exec 调用中指定要更新的命名空间:

def change_p5_v1():
    exec(f"p{index_of_p} = 3", globals())
    print(p5)  # 输出 3
英文:

You can specify the namespace you want to update in your exec call:

def change_p5_v1():
    exec(f"p{index_of_p} = 3", globals())
    print(p5)  # Output 3

答案2

得分: 1

exec() 依次运行是完全独立的;第一个 exec("global ...") 不会影响下一个 exec()

无论如何,你永远不应该使用 eval()exec(),除非你有一些_非常_具体的需求,并且作为初学者,嗯,你没有。

你似乎正在使用 p{index_of_p} 基本上是一个字典,或者“变量变量” - 由于你没有分配给 p,所以你也不需要 global

p = {0: 0, 1: 10}
index = 0

def change_p():
    p[index] += 1

print(p)
change_p()
print(p)
change_p()
print(p)
index = 1
change_p()
print(p)
英文:

exec()s run one after the other are entirely separate; the first exec("global ...") doesn't affect the next exec().

Anyway, you should never use eval() or exec() unless you have some very specific need, and as a beginner, you, well, don't.

What you seem to be doing with p{index_of_p} is basically a dict, or
"variable variables" - and since you're not assigning into p, you don't need global either.

p = {0: 0, 1: 10}
index = 0

def change_p():
    p[index] += 1

print(p)
change_p()
print(p)
change_p()
print(p)
index = 1
change_p()
print(p)

huangapple
  • 本文由 发表于 2023年3月10日 01:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定